tarfile — Tar 打包接口
优质
小牛编辑
135浏览
2023-12-01
测试 Tar 文件
# tarfile_is_tarfile.py
import tarfile
for filename in ['README.txt', 'example.tar',
'bad_example.tar', 'notthere.tar']:
try:
print('{:>15} {}'.format(filename, tarfile.is_tarfile(
filename)))
except IOError as err:
print('{:>15} {}'.format(filename, err))
从归档中读取元数据
# tarfile_getnames.py
import tarfile
with tarfile.open('example.tar', 'r') as t:
print(t.getnames())
# tarfile_getmembers.py
import tarfile
import time
with tarfile.open('example.tar', 'r') as t:
for member_info in t.getmembers():
print(member_info.name)
print(' Modified:', time.ctime(member_info.mtime))
print(' Mode :', oct(member_info.mode))
print(' Type :', member_info.type)
print(' Size :', member_info.size, 'bytes')
print()
# tarfile_getmember.py
import tarfile
import time
with tarfile.open('example.tar', 'r') as t:
for filename in ['README.txt', 'notthere.txt']:
try:
info = t.getmember(filename)
except KeyError:
print('ERROR: Did not find {} in tar archive'.format(
filename))
else:
print('{} is {:d} bytes'.format(
info.name, info.size))
从存档中提取文件
# tarfile_extractfile.py
import tarfile
with tarfile.open('example.tar', 'r') as t:
for filename in ['README.txt', 'notthere.txt']:
try:
f = t.extractfile(filename)
except KeyError:
print('ERROR: Did not find {} in tar archive'.format(
filename))
else:
print(filename, ':')
print(f.read().decode('utf-8'))
# tarfile_extract.py
import tarfile
import os
os.mkdir('outdir')
with tarfile.open('example.tar', 'r') as t:
t.extract('README.txt', 'outdir')
print(os.listdir('outdir'))
# tarfile_extractall.py
import tarfile
import os
os.mkdir('outdir')
with tarfile.open('example.tar', 'r') as t:
t.extractall('outdir')
print(os.listdir('outdir'))
# tarfile_extractall_members.py
import tarfile
import os
os.mkdir('outdir')
with tarfile.open('example.tar', 'r') as t:
t.extractall('outdir',
members=[t.getmember('README.txt')],
)
print(os.listdir('outdir'))
创建新归档
# tarfile_add.py
import tarfile
print('creating archive')
with tarfile.open('tarfile_add.tar', mode='w') as out:
print('adding README.txt')
out.add('README.txt')
print()
print('Contents:')
with tarfile.open('tarfile_add.tar', mode='r') as t:
for member_info in t.getmembers():
print(member_info.name)
使用备用归档成员名
# tarfile_addfile.py
import tarfile
print('creating archive')
with tarfile.open('tarfile_addfile.tar', mode='w') as out:
print('adding README.txt as RENAMED.txt')
info = out.gettarinfo('README.txt', arcname='RENAMED.txt')
out.addfile(info)
print()
print('Contents:')
with tarfile.open('tarfile_addfile.tar', mode='r') as t:
for member_info in t.getmembers():
print(member_info.name)
写入数据文件以外的来源
# tarfile_addfile_string.py
import io
import tarfile
text = 'This is the data to write to the archive.'
data = text.encode('utf-8')
with tarfile.open('addfile_string.tar', mode='w') as out:
info = tarfile.TarInfo('made_up_file.txt')
info.size = len(data)
out.addfile(info, io.BytesIO(data))
print('Contents:')
with tarfile.open('addfile_string.tar', mode='r') as t:
for member_info in t.getmembers():
print(member_info.name)
f = t.extractfile(member_info)
print(f.read().decode('utf-8'))
附加归档
# tarfile_append.py
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()])
使用压缩归档
# tarfile_compression.py
import tarfile
import os
fmt = '{:<30} {:<10}'
print(fmt.format('FILENAME', 'SIZE'))
print(fmt.format('README.txt', os.stat('README.txt').st_size))
FILES = [
('tarfile_compression.tar', 'w'),
('tarfile_compression.tar.gz', 'w:gz'),
('tarfile_compression.tar.bz2', 'w:bz2'),
]
for filename, write_mode in FILES:
with tarfile.open(filename, mode=write_mode) as out:
out.add('README.txt')
print(fmt.format(filename, os.stat(filename).st_size),
end=' ')
print([
m.name
for m in tarfile.open(filename, 'r:*').getmembers()
])