如何使用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 爱生日祝福语简短独特