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.

Nick T
  • 25,754
  • 12
  • 83
  • 121
Torran Dodd
  • 3
  • 1
  • 4
  • You might want to check out this answer. If memory serves me right, shutil.copy2() doesn't copy folders, only files. https://stackoverflow.com/a/13814557/7910911 – cmpgamer Sep 08 '17 at 17:09
  • Adam Hughes, It just comes up with this: PermissionError: [Errno 13] Permission denied – Torran Dodd Sep 08 '17 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 :) – Torran Dodd Sep 08 '17 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. – cmpgamer Sep 08 '17 at 17:20

2 Answers2

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.

cmpgamer
  • 352
  • 1
  • 7
  • 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. – Eryk Sun Sep 09 '17 at 00:28
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!

halfer
  • 19,824
  • 17
  • 99
  • 186
James Geddes
  • 742
  • 3
  • 10
  • 35