简体   繁体   English

如何将文件复制到python中的只读文件夹

[英]How do i copy files to a read-only folder in python

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.我知道我需要使用shutil.copyfile()但在实际尝试将文件复制到所述只读文件夹时不断收到错误消息。 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另请注意,我使用的是 Windows 11 电脑

Edit: I have also tried using os.system("attrib -r " + path) which led to the same exact error.编辑:我也尝试过使用os.system("attrib -r " + path)导致同样的错误。

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.事实证明,经过大量研究,Windows 的每个文件夹都切换了只读框,但它实际上并没有做任何事情。 Weird huh?很奇怪吧? So that wasn't really the issue.所以这不是真正的问题。 The actual issue had something to do with the shutil.copyfile() method.实际问题与shutil.copyfile()方法有关。 If you use shutil.copy2() it will work.如果您使用shutil.copy2()它将起作用。 Not sure why though.不知道为什么。 Couldn't get an explanation on that.对此无法解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM