【问题标题】:Trouble moving files from folder to folder python麻烦将文件从文件夹移动到文件夹python
【发布时间】:2014-08-20 20:25:19
【问题描述】:

我在将文件从一个文件夹移动到另一个文件夹时遇到问题。我写了这个简单的代码:

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'

如果有人可以帮助我,将不胜感激!

【问题讨论】:

  • os.path.join(dirpath,file)

标签: python copy shutil


【解决方案1】:

file 变量只是名称,要获得完整路径,请将其添加到 dirpath 变量中:

shutil.copy( os.path.join(dirpath, file), destPath )

【讨论】:

  • 注:到 OP:典型地,您使用 for root, dirs, files 迭代 os.walkdirpath 是什么?未来的程序员会感谢你! :)
【解决方案2】:
  1. 您是否拥有这些文件夹的完全访问权限?首先检查一下。
  2. 当您尝试启动脚本时,通过右键单击以管理员身份启动 Python。

我遇到了同样的问题。我就是这样解决问题的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多