Dhiwakar Ravikumar
Dhiwakar Ravikumar

Reputation: 2207

Python shutil.copy [Error 13] Permission Denied

This is the part of the code where the issue seems to arise. Am I missing some permissions ?

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

This is the error I get. I can assure you I'm not trying to copy Recycle Bin but it still fails

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'

The first time this method is called the path was created successfully but the next time I get this error. Any idea why the second time when the path exists it fails ?

UPDATE

Finally figured out my stupid mistake. THe values sent for job were FULL , INCREMENTAL & DIFFERENTIAL not INCR or DIFF. I guess in this case the value of variable contentpath was just ''. So by default if the glob does not have a valid content path it assumes the drive letter or the root path of the volume ? Because \$Recycle.Bin is always present at the root level of a volume.

Thanks for all the help folks :) :) :)

Upvotes: 0

Views: 3613

Answers (1)

Benjamin
Benjamin

Reputation: 2286

unrelated to other users holding the file open.

1) shutil.copyfile copies ONE FILE at a time. 2) use glob.glob to find the desired set of files to be copied

# 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) 

Upvotes: 1

Related Questions