我期待通過網絡目錄遞歸搜索以確定zip
文件中的所有.xls
文件。對於在zip
文件中找到的每個XLS
文件,我想將其複製到C:
的本地位置。這是我的腳本到目前爲止:在Python中複製zipfile的內容
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)
有什麼建議嗎?
'ROOTPATH =「L:\ DATA \」。?'將最後一個「(雙引號)轉義字符 – Nilesh
您可以編輯和顯示錯誤這將有助於 –
有輕微的變化編輯和最新的錯誤 – thefragileomen