如何使用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文件,并将它们存储在当前目录中。
热门推荐
4 初八的祝福语简短
10 兄弟的结婚祝福语简短
11 生日油画棒祝福语简短
12 对同事简短生日祝福语
13 白羊生日祝福语 简短独特
14 分手快乐祝福语简短
15 简短古文生日祝福语
16 冬季祝福语大全 简短的
17 表达家人的祝福语简短
18 给舍友送祝福语简短