当前位置: 首页>>代码示例>>Python>>正文


Python TarFile.next方法代码示例

本文整理汇总了Python中tarfile.TarFile.next方法的典型用法代码示例。如果您正苦于以下问题:Python TarFile.next方法的具体用法?Python TarFile.next怎么用?Python TarFile.next使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tarfile.TarFile的用法示例。


在下文中一共展示了TarFile.next方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import next [as 别名]
def main(argv):
    import getopt
    def usage():
        print('usage: %s [-b basedir] cmd [arg ...]' % argv[0])
        return 100
    try:
        (opts, args) = getopt.getopt(argv[1:], 'db:')
    except getopt.GetoptError:
        return usage()
    debug = 0
    basedir = 'tar'
    for (k, v) in opts:
        if k == '-d': debug += 1
        elif k == '-b': basedir = v
    tardb = TarDB(basedir)
    if not args: return usage()
    cmd = args.pop(0)
    if cmd == 'create':
        tardb.create()
    elif cmd == 'import':
        tardb.open()
        for path in args:
            tar = TarFile(path)
            while True:
                info = tar.next()
                if info is None: break
                fp = tar.fileobj
                fp.seek(info.offset+BLOCKSIZE)
                data = fp.read(info.size)
                tardb.add_record(info, data)
            tardb.flush()
        tardb.close()
    elif cmd == 'add':
        tardb.open()
        for path in args:
            name = os.path.basename(path)
            info = TarInfo(name)
            with open(path, 'rb') as fp:
                data = fp.read()
            recno = tardb.add_record(info, data)
            print(recno)
        tardb.close()
    elif cmd == 'get':
        tardb.open()
        for recno in args:
            recno = int(recno)
            (_, data) = tardb.get_recinfo(recno, True)
            sys.stdout.buffer.write(data)
        tardb.close()
    elif cmd == 'getinfo':
        tardb.open()
        for recno in args:
            recno = int(recno)
            (info, _) = tardb.get_recinfo(recno, False)
            print(info)
        tardb.close()
    else:
        return usage()
    return 0
开发者ID:euske,项目名称:verbose-giggle,代码行数:61,代码来源:tardb.py

示例2: main

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import next [as 别名]
def main(argv):
    import getopt
    def usage():
        print('usage: %s [-b basedir] cmd [arg ...]' % argv[0])
        return 100
    try:
        (opts, args) = getopt.getopt(argv[1:], 'db:')
    except getopt.GetoptError:
        return usage()
    debug = 0
    basedir = 'msg'
    for (k, v) in opts:
        if k == '-d': debug += 1
        elif k == '-b': basedir = v
    if not args: return usage()
    cmd = args.pop(0)
    msgdb = MessageDB(basedir)
    if cmd == 'create':
        msgdb.create()
    elif cmd == 'import':
        msgdb.open()
        for path in args:
            tar = TarFile(path)
            while True:
                info = tar.next()
                if info is None: break
                fp = tar.fileobj
                fp.seek(info.offset+BLOCKSIZE)
                data = fp.read(info.size)
                recno = msgdb.add_file(gzip2bytes(data))
                print(recno)
            msgdb.flush()
        msgdb.close()
    elif cmd == 'add':
        msgdb.open()
        for path in args:
            with open(path, 'r') as fp:
                data = fp.read()
            recno = msgdb.add_file(data)
            print(recno)
        msgdb.close()
    elif cmd == 'search':
        msgdb.open()
        for data in msgdb.search_text(args):
            print(rmsp(data)[:80])
        msgdb.close()
    else:
        return usage()
    return 0
开发者ID:euske,项目名称:verbose-giggle,代码行数:51,代码来源:msgdb.py


注:本文中的tarfile.TarFile.next方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。