python如何从zip压缩包内过滤出指后缀文件
课程设计
1
比如压缩包中有.jpg、.txt、.doc而我只想打印出.txt的文件列表,如何实现,初学pyhton还请有经验的朋友帮忙指导,我的简单代码如下:
#coding = utf-8
-*- coding: cp936 -*-
import os
import fnmatch
import zipfile
def iterfindfiles(path, fnexp):
for root, dirs, files in os.walk(path):
for filename in fnmatch.filter(files, fnexp):
yield os.path.join(root, filename)
for filelist in iterfindfiles("F:\pythinfile", "*.zip"):
print filelist #打印zip文件列表
print '#'*5
z = zipfile.ZipFile(filelist, "r")
for file in z.namelist( ):
print file #打印zip文件中文件的列表
需要在打印出所有文件信息中过滤出指定类型的文件,如何添加代码?谢谢
-
import os import fnmatch import zipfile import re def iterfindfiles(path, fnexp): for root, dirs, files in os.walk(path): for filename in fnmatch.filter(files, fnexp): yield os.path.join(root, filename) result="" for filelist in iterfindfiles("D:\Temporary", "*.zip"): z = zipfile.ZipFile(filelist, "r") for file in z.namelist(): result=result+file+"\n" print "以下内容是压缩包所包含文件:" print result+"\r\n" unicodepage=result.decode("utf-8") myItems=re.findall("([\w\d]*?.txt)",unicodepage,re.S) items=[] print "以下是获取所有以.txt结束的文本" for item in myItems: items.append(item.replace("\r\n","")) print items
发表回复