python读取文件夹下所有文件名,在Python中获取已归档文件夹内容的文件名

本文介绍如何使用Python的tarfile库处理名为gziptest.tar.gz的压缩文件,指出gziplibrary示例不包含提取文件名和内容的功能,建议先解压为tar,再利用tarfile操作。通过实例演示了tarfile.open()方法提取tar文件中的内容和文件名。
摘要由CSDN通过智能技术生成

I have a compressed folder called gziptest.tar.gz which contains several plaintext files.

I'd like to be able to get the filenames and corresponding contents of the files, but the examples of usage for the gzip library don't cover this.

The following code:

import gzip

in_f = gzip.open('/home/cholloway/gziptest.tar.gz')

print in_f.read()

produces the output:

gzip test/file2000664 001750 001750 00000000016 12621163624 015761 0ustar00chollowaycholloway000000 000000 I like apples

gzip test/file1000664 001750 001750 00000000025 12621164026 015755 0ustar00chollowaycholloway000000 000000 hello world

line two

gzip test/000775 001750 001750 00000000000 12621164026 015035 5ustar00chollowaycholloway000000 000000

I could use some regular expressions to detect the start of a new file and extract the filename, but I'm wondering if this functionality already exists within gzip or another standard python library.

解决方案

For that file, don't use the gzip library. Use the tarfile library.

The file you are working with is the gzip-compression of a tar archive of the files test/*.

If you only want to recover the tar archive, then use gzip to uncompress the file. The resulting file is (as you discovered) an archive of the files you want.

Logically, if you want to access the files inside the tar archive, we must first use the gzip library to recover the tar archive and then use the tarfile library to recover the files.

Practically, we only use the tarfile library: the tarfile library will automatically invoke the gzip library on your behalf.

I've copied this example from the examples section of the tarfile man page:

import tarfile

tar = tarfile.open("sample.tar.gz")

tar.extractall()

tar.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值