【问题标题】:shutil returns IOError: [Errno 2] No such file or directoryshutil 返回 IOError: [Errno 2] No such file or directory
【发布时间】:2017-09-06 18:16:02
【问题描述】:

我正在尝试使用 python 中的 shutil 将文本文件从一个文件夹复制到另一个文件夹。我写了以下几行:

import shutil
path_to_text= 'C:\pyprog\sample.txt'
dest = 'C:\pyprog\dest'

但每次调用此代码时,我都会得到 Errno 2。错误的回溯是

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python26\lib\shutil.py", line 88, in copy copyfile(src, dst) File "C:\Python26\lib\shutil.py", line 52, in copyfile fsrc = open(src, 'rb') IOError: [Errno 2] No such file or directory: 'C:\\pyprog\\sample.txt'

该文件存在,所以我可能做错了什么。我错在哪里?

【问题讨论】:

  • 确保您的文件未被称为sample.txt.txt。 Windows 喜欢这样做。
  • 邪恶的窗口。你是对的。谢谢。
  • 但现在我收到了{IOERROR}[errno 13] permission denied:\\pyprog
  • 如果您使用比 Windows XP 更新的软件,您可能需要以管理员身份运行脚本,因为 C: 驱动器下的大多数文件夹都被锁定。如果您想要一个读取/写入文件的好地方,我会推荐您的“我的文档”文件夹或类似文件夹。
  • 完成。谢谢大家。

标签: python windows python-2.7 ioerror shutil


【解决方案1】:

确定

shutil.copyfile(src, dst)

不是

shutil.copyfile(dst, src)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2020-03-29
    相关资源
    最近更新 更多