第八章:数据压缩与归档-tarfile:tar归档访问-追加到归档

8.4.7 追加到归档
除了创建归档,还可以使用模式’a’追加到一个现有的文件。

import tarfile

print('creating archive')
with tarfile.open('tarfile_append.tar',mode='w') as out:
    out.add('README.txt')

print('contents:')
with tarfile.open('tarfile_append.tar',mode='r') as t:
    print([m.name for m in t.getmembers()])

print('adding index.rst')
with tarfile.open('tarfile_append.tar',mode='a') as out:
    out.add('index.rst')

print('contents:')
with tarfile.open('tarfile_append.tar',mode='r') as t:
    print([m.name for m in t.getmembers()])

最后得到的归档将包含两个成员。
运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43193719/article/details/89292742