我无法将文件从一个文件夹移动到另一个文件夹。我写了这个简单的代码:
import os
import shutil
movePath = "C:\\Users\\BWhitehouse\\Documents\\GVRD\\MonthlySummary_03\\SCADA"
destPath = "I:\\eng\\GVRD\\Rain_Gauges\\MonthlyDownloads\\2014-03"
for dirpath, dirs, files in os.walk(movePath):
for file in files:
if file.endswith('.xls'):
shutil.copy(file, destPath)
这是我得到的错误:
Traceback (most recent call last):
File "C:\Python34\test.py", line 12, in <module> shutil.copy(file, destPath)
File "C:\Python34\lib\shutil.py", line 228, in copy copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Python34\lib\shutil.py", line 107, in copyfile with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'BU07-201403.xls'
如果有人能帮助我,我将不胜感激!
答案 0 :(得分:3)
file
变量只是名称,以获取完整路径将其添加到dirpath
变量:
shutil.copy( os.path.join(dirpath, file), destPath )
答案 1 :(得分:0)
我有同样的问题。我以这种方式解决了这个问题。