1

I understand that I need to use shutil.copyfile() but I keep getting an error when actually trying to copy a file to said read-only folder. I tried turning off read-only manually at the folder but it just turns itself back on.

Error: PermissionError: [Errno 13] Permission Denied: [long folder path]

I also tried running as administrator but that did nothing.

Also note that I am using a windows 11 pc

Edit: I have also tried using os.system("attrib -r " + path) which led to the same exact error.

bill
  • 21
  • 3
  • 1
    "it just turns itself back on" - how? Did turning off the read-only mode work for a moment? – lucidbrot Jul 14 '22 at 14:48
  • How was the folder created? Windows sometimes has weird permissions issues when I `rsync` a folder from linux to my windows pc. – lucidbrot Jul 14 '22 at 14:49
  • @lucidbrot The parent folder is AppData and my current theory on why it's turning itself back on is that AppData has read-only set to itself and all children files/folders. edit: oh and no it didn't work for a moment. I checked on and then looked at the property's and it changed pretty much right away. – bill Jul 14 '22 at 14:51
  • @lucidbrot the folder was created when I installed minecraft Java edition. The folder im trying to copy files to is .minecraft/mods – bill Jul 14 '22 at 14:52
  • I have no idea how windows permissions for that folder works, but it looks like [you are not the only one with this problem](https://stackoverflow.com/questions/66839646/how-to-give-python-permission-to-copy-files-in-appdata). A quick google on my part did not reveal exactly how to get appdata permission in another program's folder. Before you dive too dep into windows permissions though: Could it be that you specified the path wrong, as suggested [here](https://stackoverflow.com/a/62244490/2550406) ? – lucidbrot Jul 14 '22 at 16:45
  • @lucidbrot I don't think I've got the path wrong but it is worth looking into so I will check to make sure that's not the case. Thank you :) – bill Jul 14 '22 at 19:10

1 Answers1

1

Found an answer!

As it turns out, after lots and lots of research, every folder for windows has that read-only box toggled but it doesn't actually do anything. Weird huh? So that wasn't really the issue. The actual issue had something to do with the shutil.copyfile() method. If you use shutil.copy2() it will work. Not sure why though. Couldn't get an explanation on that.

bill
  • 21
  • 3
  • Interesting. You can view the differences [here](https://docs.python.org/3/library/shutil.html). Does a `copy` work too? One thing I notice is that `copy` and `copy2` support `dst` being a directory instead of file path. If you specified a folder, then that was the problem. They also retain the original file permissions and [there](https://docs.python.org/3/library/os.html#os.chmod) is a note that says on windows that is just the file's readonly flag. Perhaps this is important when moving to a readonly folder (?) that might otherwise propagate the readonly to the file (?) – lucidbrot Jul 15 '22 at 09:12
  • Well... looks like `copy2` is a strict superset of `copyfile`, see [here](https://github.com/python/cpython/blob/3.10/Lib/shutil.py#L421). So it must have been that you used a folder path instead of a file path. Can you confirm? – lucidbrot Jul 15 '22 at 09:17