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()])
最后得到的归档将包含两个成员。
运行结果: