Torran Dodd Torran Dodd - 3 years ago 178
Python Question

How to run a Python script with higher permissions?

I am trying to make a basic program to backup one folder from my memory stick when it is plugged in, (I know there are already programs that can do this but that is no fun!) but am having trouble with permissions.

from shutil import copy2

copy2('F:/Python/Library', 'C:/Users/Torran/Desktop/Python')


This is all I have so far, as I want to get the copying part working before doing the detecting when it is plugged in part. When I run this, however, it keeps giving me a
PermissionError
...

PermissionError: [Errno 13] Permission denied: 'F:/Python/Library'


I know that a Python script can only access folders in the same folder it is saved to, however this doesn't really help as I need to copy a folder from my memory stick and paste it into a folder on my desktop, so I need a way to give this script access to folders outside the folder it is saved to.

Answer Source

After trying it myself, I found out the problem. You are using the shutils.copy2(src, dst) function on a folder, not a file. src has to be a file. If you are trying to copy a folder to a destination folder, you need to be using shutils.copytree(src, dst).

You end up getting the permission error because shutils.copy2()expects a file.

As for the underlying question of your issue for copying a folder to a destination, please read this for a few different ways to handle this issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download