如何使用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文件,并将它们存储在当前目录中。
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便