从一个目录复制内容并粘贴到另一个目录,IOError: [Errno 13] python

0 投票
3 回答
3294 浏览
提问于 2025-04-17 17:13

我在论坛上看了很多类似的帖子,大家都遇到同样的错误,但我还是没法解决这个问题。我在使用shutil.copy()的时候,出现了IOError: [Errno 13] Permission denied: 'C:/..../.'的错误。以下是我的代码:

import subprocess, os, shutil

for i in range(1,3):
    path = 'C:/Users/TEvans/Desktop/Testing/slope%d' % i 
    if not os.path.exists(path): 
        os.makedirs(path)
    os.chdir(path)
    for j in range(1,4):
        path1 = 'C:/Users/TEvans/Desktop/Testing/slope%d/r%d' % (i, j)
        if not os.path.exists(path1): 
            os.makedirs(path1)
        src = 'C:/Users/TEvans/Desktop/Testing/PP%d/S%d' % (i, j)
        dst = 'C:/Users/TEvans/Desktop/Testing/slope%d/r%d' % (i, j)
        shutil.copy(src, dst)


Traceback (most recent call last):
  File "sutra.py", line 14, in <module>
    shutil.copy(src, dst)
  File "C:\Python27\lib\shutil.py", line 117, in copy
    copyfile(src, dst)
  File "C:\Python27\lib\shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: 'C:/Users/TEvans/Desktop/Testing/PP1/S1'

3 个回答

0

我想你可能在使用Windows电脑,可能是Windows Vista或者更新的版本。看到这个错误代码是Errno 13,我可以很肯定你没有以管理员身份运行你的脚本。试着以管理员身份运行你的脚本,或者如果你是通过命令提示符来执行脚本的话,先以管理员身份打开cmd.exe,然后再执行你的脚本。

在Windows上,建议使用反斜杠,因为你在使用Windows。同时在使用反斜杠的时候,建议使用原始字符串,也就是:

path = r'C:\Users\TEvans\Desktop\Testing\slope%d'
1

shutil.copy 是用来复制文件的,而不是复制文件夹。它的第一个参数需要是一个文件,第二个参数可以是一个文件夹或者文件名。如果第二个参数是一个文件名,它会复制这个文件并且重命名。

如果你想复制一个文件夹,最好的方法是使用 distutils's dir_util 这个库。

>>> import distutils.dir_util
>>>
>>> dir(distutils.dir_util)
['DistutilsFileError', 'DistutilsInternalError', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__revision__', '_build_cmdtuple', '_path_created', 'copy_tree', 'create_tree', 'ensure_relative', 'errno', 'log', 'mkpath', 'os', 'remove_tree']
>>>

copy_tree 函数可以帮助你复制整个文件夹。

请参考下面的定义。

>>> help(distutils.dir_util.copy_tree)
Help on function copy_tree in module distutils.dir_util:

copy_tree(src, dst, preserve_mode=1, preserve_times=1, preserve_symlinks=0, upda
te=0, verbose=1, dry_run=0)
    Copy an entire directory tree 'src' to a new location 'dst'.

    Both 'src' and 'dst' must be directory names.  If 'src' is not a
    directory, raise DistutilsFileError.  If 'dst' does not exist, it is
    created with 'mkpath()'.  The end result of the copy is that every
    file in 'src' is copied to 'dst', and directories under 'src' are
    recursively copied to 'dst'.  Return the list of files that were
    copied or might have been copied, using their output name.  The
    return value is unaffected by 'update' or 'dry_run': it is simply
    the list of all files under 'src', with the names changed to be
    under 'dst'.

    'preserve_mode' and 'preserve_times' are the same as for
    'copy_file'; note that they only apply to regular files, not to
    directories.  If 'preserve_symlinks' is true, symlinks will be
    copied as symlinks (on platforms that support them!); otherwise
    (the default), the destination of the symlink will be copied.
    'update' and 'verbose' are the same as for 'copy_file'.

>>>
3

shutil.copy 是用来复制单个文件的。如果你想要复制整个文件夹(目录),你应该使用 shutil.copytree,这个函数可以递归地复制整个目录及其内容。

import subprocess, os, shutil

for i in range(1,3):
    path = 'C:/Users/TEvans/Desktop/Testing/slope%d' % i 
    if not os.path.exists(path): 
        os.makedirs(path)
    os.chdir(path)
    for j in range(1,4):
        src = 'C:/Users/TEvans/Desktop/Testing/PP%d/S%d' % (i, j)
        dst = 'C:/Users/TEvans/Desktop/Testing/slope%d/r%d' % (i, j)
        shutil.copytree(src, dst)

撰写回答