2013-02-26 102 views
0

我已經通過論壇在類似這個w /同樣的錯誤的幾個帖子看過,但我仍然無法修復它。我得到IOError:[Errno 13] Permission denied:'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複製整個目錄:

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

那麼你也應該知道什麼是用於複製目錄 – 2016-05-25 07:04:28

+0

@AhmadTaha更新了答案使用反斜槓。 – kvivek 2016-05-25 16:12:49

+0

立即響應和工作upovte – 2016-05-25 16:30:58

0

我想你是在Windows機器上可能在Windows Vista或更高版本;看到這是Errno 13我很確定你沒有以管理員身份運行你的腳本。嘗試以管理員身份運行腳本,或者如果通過命令提示符執行腳本,請嘗試以管理員身份運行cmd.exe,然後執行它。

嘗試,因爲你在窗口和同時使用反斜線嘗試使用原始字符串即

path = r'C:\Users\TEvans\Desktop\Testing\slope%d' 
相關問題