我正在开发一个程序,该程序应该从tarfile中获取信息(大小和名称)。现在我不知道如何从对象中获取tarfile的信息。那是我的代码:
import tarfile
from io import BytesIO
import re
def open_tars_logic(name_s, tar_bytes):
filedata = BytesIO(tar_bytes.read()) # Here I need a solution
with tarfile.open(fileobj=filedata) as new_tar:
all_names = new_tar.getnames()
for name in new_tar.getnames()
if re.search(r'\.tar\.gz$', name) is not None:
name_f = open_tars_logic(name, tar_bytes)
all_names.append(name_f)
return all_names
# "tar_file" is the path to the tarfile
def open_tar(tar_file):
with open(tar_file, "rb") as tar_bytes:
with tarfile.open(tar_file, "r") as parent_tar:
all_names = parent_tar.getnames()
for name in parent_tar.getnames():
if re.search(r'\.tar\.gz$', name) is not None: # Search for other Tarfiles
name_f = open_tars_logic(name, tar_bytes)
all_names.append(name_f)
return all_names
我暂时忽略大小。
这里是目标文件的示例:
嘿,获取有关任何tarfile的信息:
import tarfile
import time
t = tarfile.open('example.tar.gz', 'r')
for info in t.getmembers():
print(info.name)
print('Modified:', time.ctime(info.mtime))
print('Mode :', oct(info.mode))
print('Type :', info.type)
print('Size :', info.size, 'bytes')
更多信息,请阅读此文档:https://www.journaldev.com/17946/python-tarfile-module