2014-12-04 247 views
0

這是問題似乎出現的代碼的一部分。我是否缺少一些權限?Python shutil.copy [錯誤13]權限被拒絕

def addcontent(job,jobpath): 
    contentpath='' 
    if job == "FULL": 
     print('This job will copy data from '+full_contentpath) 
     contentpath=str(full_contentpath) 
    elif job == "INCR": 
     print('This job will copy data from '+incr_contentpath) 
     contentpath=str(incr_contentpath) 
    elif job == "DIFF": 
     print('This job will copy data from '+diff_contentpath) 
     contentpath=str(diff_contentpath) 
    else: 
     pass 
    os.makedirs(jobpath) 
    for file in glob.iglob(contentpath+r'\*'): 
     shutil.copy(file,jobpath) 

''' Method versionfile() will be called only for bkp_witout_inp() methods 
'''   
def versionfile(): 
     global mainpath 
     #we will also create a file for checking versioning - Every job will contain a new version of this file. Prior to running this job we shall touch it to get a new version 
     if os.access(mainpath+r"\versionfile.txt",os.F_OK): 
      os.system(r"copy /b "+"\""+mainpath+r"\versionfile.txt"+"\""+r" +,,") 
     else: 
      os.system(r"fsutil file createnew "+mainpath+r"\versionfile.txt 10240000") 
     # the above if..else clause will modfiy the file if it exists 

這是我得到的錯誤。我可以向你保證,我不會試圖複製回收站,但它仍然無法

Traceback (most recent call last): 
    File "create&&bkp.py", line 26, in <module> 
    BackupSC.bkp_witout_inpv10() 
    File "C:\Users\dhiwakarr\workspace\basics\BackupSC.py", line 210, in bkp_witout_inpv10 
    addcontent(job,jobpath) # Add some Content : We need Job Type & Path to Create as parameters 
    File "C:\Users\dhiwakarr\workspace\basics\BackupSC.py", line 96, in addcontent 
    shutil.copy(file,jobpath) 
    File "C:\Python34\lib\shutil.py", line 228, in copy 
    copyfile(src, dst, follow_symlinks=follow_symlinks) 
    File "C:\Python34\lib\shutil.py", line 107, in copyfile 
    with open(src, 'rb') as fsrc: 
PermissionError: [Errno 13] Permission denied: '\\$Recycle.Bin' 

第一次調用此方法的路徑已成功創建,但下一次我得到這個錯誤。任何想法爲什麼第二次當路徑存在它失敗?

UPDATE

終於想通了,我的愚蠢的錯誤。發送給工作的數值爲FULL,INCREMENTAL & DIFFERENTIAL不是INCR或DIFF。我猜在這種情況下,變量contentpath的值就是''。因此,默認情況下,如果glob沒有有效的內容路徑,它將假定卷的盤符或根路徑?因爲\ $ Recycle.Bin始終存在於卷的根級別。

感謝所有幫助人:) :) :)

+0

你有讀寫權限的文件? – Hackaholic 2014-12-04 09:52:51

+0

哪些文件?我正在複製的文件?是的,我願意。我能夠通過資源管理器和\\ $​​ Recycle.Bin複製它們不是我想要複製的東西。我以爲它試圖刪除一些東西,但我甚至沒有刪除 – 2014-12-04 09:58:17

+0

作爲管理員運行python腳本 – Hackaholic 2014-12-04 10:02:02

回答

1

與其他持有該文件的用戶無關。

1)shutil.copyfile一次只複製一個文件。 2)使用glob.glob找到想要的一組要拷貝的文件

# We'll be using this function to specify the buffer size: 


def copyLargeFile(src, dest,buffer_size=16000): 
     with open(src, 'rb') as fsrc: 
      with open(dest, 'wb') as fdest: 
        shutil.copyfileobj(fsrc, fdest, buffer_size) 
相關問題