蟒蛇.IOError:[Errno 13]权限被拒绝:当我正在复制文件时

G-7*_*-71 26 python windows windows-7

我有两个文件夹: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'
Run Code Online (Sandbox Code Playgroud)

有什么问题?

Tim*_*ker 60

阅读文档:

shutil.copyfile(src, dst)

将名为src的文件的内容(无元数据)复制到名为dst的文件中.dst必须是完整的目标文件名 ; 查看copy() 接受目标目录路径的副本.

  • 我认为正如蒂姆所说,它应该是完整路径,意思是shutil.copyfile(src, dst) // 源和目标也应该包括目录和文件名。 (5认同)
  • 我试过`shutil.copy`但仍面临同样的错误. (3认同)

小智 13

使用shutil.copy而不是shutil.copyfile

例:

shutil.copy(PathOf_SourceFileName.extension,TargetFolderPath)
Run Code Online (Sandbox Code Playgroud)


Gow*_*amy 7

使用shutil.copy2代替shutil.copyfile

import shutil 
shutil.copy2('/src/dir/file.ext','/dst/dir/newname.ext') # file copy to another file
shutil.copy2('/src/file.ext', '/dst/dir') # file copy to diff directory
Run Code Online (Sandbox Code Playgroud)