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个回答

93

请阅读 文档

shutil.copyfile(src, dst)

复制名为 src 的文件的内容(不包括元数据)到名为 dst 的文件中。 dst 必须是 完整的目标文件名;查看 copy() 可以接受目标目录路径的复制。


6
我尝试了shutil.copy,但仍然遇到了相同的错误。 - Pyd
6
我认为像Tim所说的那样,它应该是完整路径,意思是shutil.copyfile(src, dst) //源和目标应该包括目录和文件名。 - emir

26

请使用shutil.copy而不是shutil.copyfile。

示例:

shutil.copy(PathOf_SourceFileName.extension,TargetFolderPath)

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

4

我解决了这个问题,你需要将完整的目标文件名作为目标

目标 = 路径目录 + 文件名.*

我使用以下代码使用shutil复制wav文件:

    # open file with QFileDialog

    browse_file = QFileDialog.getOpenFileName(None, 'Open file', 'c:', "wav files (*.wav)")

    # get file name 

    base = os.path.basename(browse_file[0])
    os.path.splitext(base)
    print(os.path.splitext(base)[1])

    # make destination path with file name

    destination= "test/" + os.path.splitext(base)[0] + os.path.splitext(base)[1]
    shutil.copyfile(browse_file[0], destination)

1
首先,请确保您的文件没有被Windows或某些应用程序锁定,例如MS Office会锁定已打开的文件。
我在试图重命名目录中的长文件列表时遇到了错误13,但Python尝试重命名与我的文件相同路径下的一些文件夹。因此,如果您没有使用shutil库,请检查它是一个目录还是文件!
import os
path="abc.txt"

if os.path.isfile(path):
    #do yor copy here
    print("\nIt is a normal file") 

或者

if os.path.isdir(path):
    print("It is a directory!")
else:
    #do yor copy here
    print("It is a file!")

0

使用

> from shutil import copyfile
> 
> copyfile(src, dst)

使用src和dst:

srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)

0

Visual Studio 2019

解决方案:管理员已经为此文件夹 "C:\ProgramData\Docker" 提供了完全访问权限,现在可以正常工作。

错误:在将文件复制到卷 edgehubdev 时出现文件 IO 错误。错误号: 13,错误信息 Permission denied : [Errno 13] Permission denied: 'C:\ProgramData\Docker\volumes\edgehubdev\_data\edge-chain-ca.cert.pem' [ERROR]: 运行 'iotedgehubdev start -d "C:\Users\radhe.sah\source\repos\testing\AzureIotEdgeApp1\config\deployment.windows-amd64.json" -v' 失败,错误提示:警告!通过 CLI 使用 --password 不安全,请使用 --password-stdin。 错误:在将文件复制到卷 edgehubdev 时出现文件 IO 错误。错误号: 13,错误信息 Permission denied : [Errno 13] Permission denied: 'C:\ProgramData\Docker\volumes\edgehubdev\_data\edge-chain-ca.cert.pem'


这是唯一对我有效的方法。谢谢兄弟。 - Carlost

0

确保你不在任何你试图使用shutil.copy的文件中(已锁定)。

这应该有助于解决你的问题。


0
这对我有效:
import os
import shutil
import random


dir = r'E:/up/2000_img'
output_dir = r'E:/train_test_split/out_dir'


files = [file for file in os.listdir(dir) if os.path.isfile(os.path.join(dir, file))]

if len(files) < 200:
    # for file in files:
    #     shutil.copyfile(os.path.join(dir, file), dst)
    pass
else:
    # Amount of random files you'd like to select
    random_amount = 10
    for x in range(random_amount):
        if len(files) == 0:
            break
        else:
            file = random.choice(files)
            shutil.copyfile(os.path.join(dir, file), os.path.join(output_dir, file))

-1

我通过以下方式避免了这个错误:

  1. 导入库'pdb'并在'shutil.copyfile'之前插入'pdb.set_trace()',代码如下:
  import pdb
    ...
    print(dst)
    pdb.set_trace()
    shutil.copyfile(src,dst)
  1. 在终端中运行Python文件,它将执行到'pdb.set_trace()'这一行,现在'dst'文件将被打印出来。

  2. 自己复制'src'文件,并替换和删除上述代码创建的'dst'文件。

  3. 然后在终端中输入'c'并点击'Enter'键以执行以下代码。


这与问题有什么关系?你建议使用调试器来运行一个复制函数,否则它就会出现魔法般无法工作的情况? - chrslg

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