我是Python新手,似乎不能让它工作。
下面的代码能够找到ZIP文件并提取出
只要一个文件夹中只有一个ZIP文件。问题似乎是zipfile.zipfile文件从第一个函数中读取返回值作为一个大字符串,而不是路径,我用尽了ide如何修复这个问题。在import zipfile
import os
import fnmatch
def archive1():
myarchive = []
rootPath= (r'E:\Test\2017')
pattern = '*.zip'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files,pattern):
zipfile.ZipFile(os.path.join(root, filename))
myarchive.append(os.path.join(root,filename))
return str(myarchive).replace('[',"").replace(']',"").replace('"',"").replace("'","")
#this is here so function returns as string and replace characters so second function reads it as applicable path(s).
if __name__ == '__main__':
archive1()
myarchive1 = archive1()
def extractor():
new_dr = r'E:\Test'
extensions = ('.txt','.pdf')
zip_file = zipfile.ZipFile(myarchive1)
print (zip_file)
[zip_file.extract(file,new_dr) for file in zip_file.namelist() if file.endswith(extensions)]
zip_file.close()
if __name__ == '__main__':
extractor()
我得到:
^{pr2}$