Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从一个目录复制内容并粘贴到另一个目录,IOError:[Errno 13]python_Python_Copy_Shutil - Fatal编程技术网

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

从一个目录复制内容并粘贴到另一个目录,IOError:[Errno 13]python,python,copy,shutil,Python,Copy,Shutil,我在论坛上浏览过几篇类似的帖子,都有相同的错误,但仍然无法修复。使用shutil.copy()时,我收到IOError:[Errno 13]权限被拒绝:“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.ma

我在论坛上浏览过几篇类似的帖子,都有相同的错误,但仍然无法修复。使用shutil.copy()时,我收到IOError:[Errno 13]权限被拒绝:“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'
导入子流程、操作系统、shutil
对于范围(1,3)内的i:
路径='C:/Users/TEvans/Desktop/Testing/slope%d'%1!'
如果操作系统路径不存在(路径):
os.makedirs(路径)
os.chdir(路径)
对于范围(1,4)内的j:
路径1='C:/Users/TEvans/Desktop/Testing/slope%d/r%d'(i,j)
如果操作系统路径不存在(路径1):
os.makedirs(路径1)
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.副本(src、dst)
回溯(最近一次呼叫最后一次):
文件“sutra.py”,第14行,在
shutil.副本(src、dst)
文件“C:\Python27\lib\shutil.py”,第117行,副本
复制文件(src、dst)
copyfile中第82行的文件“C:\Python27\lib\shutil.py”
开放式(src,'rb')作为fsrc:
IOError:[Errno 13]权限被拒绝:“C:/Users/TEvans/Desktop/Testing/PP1/S1”

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)

复制一个文件。您希望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)

copy用于复制文件而不是目录。它需要一个文件作为第一个参数,目录或文件名作为第二个参数。如果第二个参数是文件名,它将复制并重命名该文件

要复制目录,最好的方法是使用
distutils的dir\u 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']
>>>
复制树功能将帮助您复制整个目录

请参阅以下定义

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

>>>

copy用于复制文件而不是目录。它需要一个文件作为第一个参数,目录或文件名作为第二个参数。如果第二个参数是文件名,它将复制并重命名该文件

要复制目录,最好的方法是使用
distutils的dir\u 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']
>>>
复制树功能将帮助您复制整个目录

请参阅以下定义

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

>>>

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

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

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

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

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

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

然后,你还应该告诉什么是用来复制一个文件directory@AhmadTaha更新了答案。然后,您还应该说明用于复制的内容directory@AhmadTaha更新了答案。