0

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.

4
  • You might want to check out this answer. If memory serves me right, shutil.copy2() doesn't copy folders, only files. stackoverflow.com/a/13814557/7910911 Commented Sep 8, 2017 at 17:09
  • Adam Hughes, It just comes up with this: PermissionError: [Errno 13] Permission denied Commented Sep 8, 2017 at 17:11
  • cmpgamer, Thank you, I have fixed it and just decided to copy the files inside the folder, this works fine now - cheers :) Commented Sep 8, 2017 at 17:18
  • I followed up with an actual answer once I gave it a go. I personally prefer using the os module rather than shutils because Python hides a lot of the shell utilities behind the scenes. Commented Sep 8, 2017 at 17:20

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

At issue is a weird Windows API mapping from the kernel's sensible STATUS_FILE_IS_A_DIRECTORY status code to ERROR_ACCESS_DENIED, which repeatedly misleads novice programmers down the path of file permissions and running Python as an administrator.
0

I would suggest taking a look at "Run python script as admin in Windows", as this answer explains how to force extra admin permissions. Try that, then should it not work the problem will likely lie with the command as cmpgamer says.

By the way, welcome to Python and programming in general! It is a great world to get into as it lets you achieve so much in so many fields. Python is "the" language to know right now as it is very powerful and quick to dev. Have you tried working with the Raspberry Pi? You can do some very fun Python projects on them! Backup, as you are describing, can be achieved with Windows Shell scripting, whereas you can do AI in python!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.