I am using shutil.copy to copy files from one location to another. If a file with the same name already exists in the destination location, it is normally ok and overwrites. However, if the destination is read-only, it throws a permission denied error.
What is the most elegant way to deal with this? Is there another shutil function that will deal with the permissions issue, or must I check the permissions on ever file I copy?
smth like
import os
import shutil
def my_super_copy(what, where):
try:
shutil.copy(what, where)
except IOError:
os.chmod(where, 777) #?? still can raise exception
shutil.copy(what, where)
You don't have to check for permissions. Let the OS tell you there's a permission problem and then deal with it. I'm assuming that the PermissionDeniedError is the exception you're getting so your solution would look something like this.
try:
shutil.copy(blah,blah,blah)
except PermissionDeniedError:
<Code for whatever you want to do if there arent sufficient permissions>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With