Python. IOError: [Errno 13] Permission denied: 当我复制文件时

50

我有两个文件夹:In和Out - 它们不是D盘上的系统文件夹 - Windows 7。Out文件夹中包含“myfile.txt”文件。我在Python中运行以下命令:

>>> shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )

Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )
  File "C:\Python27\lib\shutil.py", line 82, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: 'D:\\In'

有什么问题?


1
使用资源管理器,我可以将myfile.txt复制到In文件夹中。 - G-71
请确保在操作目标文件之前已经关闭了它。 - Demetry Pascal
11个回答

-3

这个问题很旧了,对于新的Python 3.6观众,请使用

shutil.copyfile( "D:\Out\myfile.txt", "D:\In" )

替代

shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )

r参数用于读取文件而不是复制


7
这个答案是错误的。r 表示_原始字符串_,意思是字符串内部的“\”字符被视为普通字符“\”,不需要进行转义处理。 - Arboreal Shark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接