2013-02-26 5 views
0

같은 오류와 비슷한 여러 게시물에서 포럼을 살펴 봤지만 여전히 수정할 수 없습니다. IOError : [Errno 13] 권한이 거부되었습니다 : 'C : /..../.' shutil.copy()를 사용할 때. 다음은 코드입니다.한 디렉터리에서 내용을 복사하여 다른 IOError : [Errno 13] python

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

shutil.copy 파일을 복사합니다. 당신은 shutil.copytree이 recurisvely 전체 디렉토리를 복사 할 :

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

>>> 
+0

그런 다음 당신은 또한 대답을 업데이트 디렉토리 –

+0

@AhmadTaha을 복사하는 데 사용됩니다 무엇을 얘기해야 원시 문자열을 사용하려고 원인

봅니다 백 슬래시를 사용합니다. 즉시 응답 및 작업을 위해 – kvivek

+0

upovte –

0

아마도 Windows Vista 또는 그 이후 버전 일 것입니다. Errno입니다. 저는 관리자로서 스크립트를 실행하지 않았 음을 확신합니다. 관리자로 스크립트를 실행하거나 명령 프롬프트를 통해 스크립트를 실행하는 경우 관리자로 cmd.exe를 실행하고 실행하십시오. 당신은 윈도우와 백 슬래시를 사용하는 동안 있습니다 즉

path = r'C:\Users\TEvans\Desktop\Testing\slope%d' 
관련 문제