我试图读取一个 tar 文件,识别一些文件,读取它们,然后使用 Python 将一个新文件写入同一个 tar 文件。看来只有模式为“r”时才允许 extractfile() 。是这样吗?有没有办法既从内存中的 tar 中提取文件,又同时将新文件附加到 tar 中?示例代码如下:
def genEntry(tar, tarinfo, source):
heading = re.compile(r'#+(\s+)?')
f = tar.extractfile(tarinfo)
f.seek(0)
while True:
line = f.readline().decode()
print(line)
if not line:
break
print(line)
if heading.match(line):
title = heading.sub('',line).replace('\n','')
return[tarinfo.name.replace(source,'.'), title]
return [tarinfo.name.replace(source,'.'), tarinfo.name.replace(source,'')]
with tarfile.open(args.source, mode='a') as tar:
source = 'somepath'
subDir = 'someSubDir'
path = '/'.join((source, subDir))
if tar.getmember(path):
pathre = re.compile(r'{}\/.+?\/readme\.md'.format(re.escape(path)), re.IGNORECASE)
for tarinfo in tar.getmembers():
if re.search(pathre, tarinfo.name):
genEntry(tar, tarinfo, source)
...
这将产生以下错误:
OSError:模式“a”操作错误
据我所知,不可能一次性读取和追加到 tar 文件。虽然我最终朝着促进 tarfile 流进出 Python 脚本的方向发展,但我确实为上述问题找到了一个两遍读/写解决方案。
这基本上就是我采用的方法。
files = []
with tarfile.open(tarpath) as tar:
files= readTar(tar)
with tarfile.open(tarpath, mode='a') as tar:
for fileobj in files:
writeFile(tar, fileobj[0], fileobj[1])
def readTar(tar):
# Your your logic to build the files you want to build in the amended file here
def writeFile(tar, tarinfo, payload):
if len(payload) != 0:
data = payload.encode('utf8')
tarinfo.mode = 0o444
tarinfo.size = len(data)
tar.addfile(tarinfo, fileobj=BytesIO(data))
return tar