[英]Copy content of zipfile in Python
我正在尋找通過網絡目錄遞歸搜索以標識zip
文件中的所有.xls
文件。 對於在zip
文件中找到的每個XLS
文件,我想將其復制到C:
的本地位置。 到目前為止,這是我的腳本:
import os
import zipfile
import fnmatch
import shutil
rootPath = "L:\Data\Cases"
destPath = "C:\Test"
allFileList = []
zipList = []
# Create a list containing all files contained within L:\Data\Cases
for dirname, dirnames, filenames in os.walk(rootPath):
for filename in filenames:
allFileList.append(os.path.join(dirname, filename))
# Return a list of filepaths containing zipfiles.
for file in allFileList:
if file.endswith(".zip"):
zipList.append(file)
for file in zipList:
with zipfile.ZipFile(file) as zip_file:
for member in zip_file.namelist():
if member.endswith(".xls"):
filename = os.path.basename(member)
if not filename:
continue
source = zip_file.open(member)
target = os.path.join(destPath, filename)
shutil.copy2(source, target)
錯誤代碼如下。 我認為該錯誤是由於將壓縮容器中的文件實際復制到目標路徑而引起的。
Traceback (most recent call last):
File "C:/Users/user/Desktop/parsecsv.py", line 30, in <module>
shutil.copy2(source, target)
File "C:\Program Files\Python278\lib\shutil.py", line 130, in copy2
copyfile(src, dst)
File "C:\Program Files\Python278\lib\shutil.py", line 68, in copyfile
if _samefile(src, dst):
File "C:\Program Files\Python278\lib\shutil.py", line 63, in _samefile
return (os.path.normcase(os.path.abspath(src)) ==
File "C:\Program Files\Python278\lib\ntpath.py", line 487, in abspath
path = _getfullpathname(path)
有什么建議么?
ZipFile.open()
不返回文件系統路徑,而是返回類似文件的ZipExtFile
對象。 您想要的是ZipFile.extract()
(然后您根本不需要shutil.copy()
):
# NB : untested code, refer to the doc for more infos
for file in zipList:
with zipfile.ZipFile(file) as zip_file:
for member in zip_file.namelist():
if member.endswith(".xls"):
zip_file.extract(member, destPath)
同樣,對於FWIW,您不需要先構建所有文件的列表,然后構建zipfile的列表,然后在此列表上進行迭代-您也可以一遍就完成整個工作:
for dirname, dirnames, filenames in os.walk(rootPath):
for filename in filenames:
if not filename.endswith(".zip"):
continue
fullpath = os.path.join(dirname, filename))
with zipfile.ZipFile(fullpath) as zip_file:
for member in zip_file.namelist():
if member.endswith(".xls"):
zip_file.extract(member, destPath)
如bruno所述,我認為您無法檢查zipfile內容,但我認為可以采用一種更簡潔的方式在提取后將其刪除,因此您可以使用shutil.rmtree刪除其他內容。
def main():
rootPath = "C:\\rootpath"
destPath = "C:\\Test"
allFileList = []
zipList = []
# Create a list containing all files contained within L:\Data\Cases
for dirname, dirnames, filenames in os.walk(rootPath):
for filename in filenames:
allFileList.append(os.path.join(dirname, filename))
# Return a list of filepaths containing zipfiles.
for file in allFileList:
if file.endswith(".zip"):
zipList.append(file)
for file in zipList:
with zipfile.ZipFile(file) as zip_file:
for member in zip_file.namelist():
if member.endswith(".xls"):
zip_file.extract(member, destPath)
for dirname, dirnames, filenames in os.walk(destPath):
for filename in filenames:
if not filename.endswith(".xls"):
shutil.rmtree(filename)
if __name__ == '__main__':
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.