0

我已经在论坛中查看了与此类似的几个帖子,但出现了相同的错误,但仍然无法修复它。我收到 IOError: [Errno 13] Permission denied: 'C:/..../.' 使用 shutil.copy() 时。这是代码:

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'
4

3 回答 3

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)
于 2013-02-26T21:07:20.413 回答
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'.

>>>
于 2013-02-26T21:14:54.433 回答
0

我猜您可能在 Windows Vista 或更高版本上的 Windows 机器上;看到这是 Errno 13,我很确定您没有以管理员身份运行脚本。尝试以管理员身份运行脚本,或者如果您通过命令提示符执行脚本,请尝试以管理员身份运行 cmd.exe 然后执行它。

尝试使用反斜杠,因为您在 Windows 中,并且在使用反斜杠时尝试使用原始字符串,即

path = r'C:\Users\TEvans\Desktop\Testing\slope%d'
于 2014-01-02T14:13:13.347 回答