如何使用Python从zip文件中提取所有.txt文件?
为了从zip提取所有.txt文件,您需要遍历zipfile中的所有文件,检查文件是否为txt文件。如果它是txt文件,则将其解压缩。为此,我们将使用zipfile模块及其提取功能。
例如
import zipfile
my_zip = zipfile.Zipfile('my_zip_file.zip') # Specify your zip file's name here
storage_path = '.'
for file in my_zip.namelist():
if my_zip.getinfo(file).filename.endswith('.txt'):
my_zip.extract(file, storage_path) # extract the file to current folder if it is a text file运行上面的代码将打开my_zip_file.zip并从中提取所有txt文件,并将它们存储在当前目录中。
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短