Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取python中tgz文件的目录结构_Python_Gzip_Listdir - Fatal编程技术网

获取python中tgz文件的目录结构

获取python中tgz文件的目录结构,python,gzip,listdir,Python,Gzip,Listdir,我有一个.tgz文件,其中包含多个文本文件。我可以使用gzip模块在Python中阅读它,我看到第一行包含了一些关于后续文件的信息,但是不清楚如何在Python中正确地迭代这些文件。我希望能够做到以下几点: for file in tgzFile: read file do stuff for file 我可以读取gzip文件的每一行,并且我可以尝试从该行的内容中识别文件的开头,但是我更喜欢更干净的方法。谢谢 import tarfile tar = tarfile.open("fil

我有一个.tgz文件,其中包含多个文本文件。我可以使用gzip模块在Python中阅读它,我看到第一行包含了一些关于后续文件的信息,但是不清楚如何在Python中正确地迭代这些文件。我希望能够做到以下几点:

for file in tgzFile:
  read file
  do stuff for file
我可以读取gzip文件的每一行,并且我可以尝试从该行的内容中识别文件的开头,但是我更喜欢更干净的方法。谢谢

import tarfile
tar = tarfile.open("file.tgz")
for file in tar.getmembers():
    print file.name
getmembers返回可以相应使用的TarInfo对象列表。

getmembers返回可以相应使用的TarInfo对象列表。