Python shutil.copy [错误13]权限被拒绝

时间:2014-12-04 09:51:35

标签: python

这是代码中似乎出现问题的部分。我错过了一些权限吗?

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'

第一次调用此方法时,路径已成功创建,但下次出现此错误时。知道为什么路径存在的第二次失败了吗?

更新

终于找到了我的愚蠢错误。发送给工作的价值是FULL,INCREMENTAL&amp; DIFFERENTIAL不是INCR或DIFF。我想在这种情况下,变量 contentpath 的值只是''。因此,默认情况下,如果glob没有有效的内容路径,它会假定驱动器号或卷的根路径?因为\ $ Recycle.Bin始终存在于卷的根级别。

感谢所有帮助人员:) :) :)

1 个答案:

答案 0 :(得分: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)