PermissionError: [WinError 5] when running script
PermissionError: [WinError 5] when running script
我收到代码片段底部注释掉的错误。
我正在 运行 闲置所有这些。我在 windows 并尝试以管理员身份在 IDLE 上右键单击 运行,我认为这可能有帮助,但它不起作用。我以前 运行 编写这样的代码来完成相同的任务,但在不同的文件夹中。我也知道还有其他更简单的方法来移动文件,但由于我正在按照书中的练习进行操作,所以我想实现以尽可能接近的方式将多个文件移动到不同目录的最终目标到我在下面描述的内容。具体来说,我正在寻找有关导致此错误的原因的信息并了解原因。
我看到了 this 视频,即将到 [7:40]。上下文略有不同,但错误消息是相同的。尽管如此,我还是看不出解释与我正在做的事情有何关系,特别是在我的 python 上下文中。任何帮助将非常感激!
import pathlib
import shutil
home = pathlib.Path.home()
pictures = home / "Pictures"
imgfiles = pictures / "imgfiles"
file1 = pictures / "image1.png"
file2 = pictures / "image2.gif"
file3 = pictures / "Uplay" / "image3.png"
file4 = pictures / "camera roll" / "image4.jpg"
filelist = [file1, file2, file3, file4]
sourcefiles = []
destination = imgfiles
for file in filelist:
file.touch()
for file in pictures.rglob("image?.???"):
sourcefiles.append(file)
for path in sourcefiles:
path.replace(destination)
回溯看起来如下:
Obtain the following Error when running the last line of code: PermissionError: [WinError 5]
Traceback (most recent call last):
File "<pyshell#42>", line 2, in <module>
path.replace(destination)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38\lib\pathlib.py", line 1366, in replace
self._accessor.replace(self, target)
PermissionError: [WinError 5] Acesso negado: 'C:\Users\XXXX\Pictures\image1.png' -> 'C:\Users\XXXX\Pictures\imgfiles'
您正试图将文件作为目录写入,Windows 不喜欢这样。如果您将目标替换为目标文件名,它就可以正常工作。
所以如果我要重写你的代码,我会这样做:-
import pathlib
import shutil
home = pathlib.Path.home()
pictures = home / "Pictures"
imgfiles = pictures / "imgfiles"
file1 = pictures / "image1.png"
file2 = pictures / "image2.gif"
file3 = pictures / "Uplay" / "image3.png"
file4 = pictures / "camera roll" / "image4.jpg"
filelist = [file1, file2, file3, file4]
sourcefiles = []
destination = imgfiles
for file in filelist:
file.touch()
for file in pictures.rglob("image?.???"):
destination=imgfiles/ str(file)[str(file).rfind("\")+1:] #Added
file.replace(destination)
#Commented
#for path in sourcefiles:
# path.replace(destination)
希望对您有所帮助。
替换:
for path in sourcefiles:
path.replace(destination)
与:
for path in sourcefiles:
path.replace(destination / path.name)
感谢来自 RealPython 的 Bart 的回答。
我收到代码片段底部注释掉的错误。 我正在 运行 闲置所有这些。我在 windows 并尝试以管理员身份在 IDLE 上右键单击 运行,我认为这可能有帮助,但它不起作用。我以前 运行 编写这样的代码来完成相同的任务,但在不同的文件夹中。我也知道还有其他更简单的方法来移动文件,但由于我正在按照书中的练习进行操作,所以我想实现以尽可能接近的方式将多个文件移动到不同目录的最终目标到我在下面描述的内容。具体来说,我正在寻找有关导致此错误的原因的信息并了解原因。
我看到了 this 视频,即将到 [7:40]。上下文略有不同,但错误消息是相同的。尽管如此,我还是看不出解释与我正在做的事情有何关系,特别是在我的 python 上下文中。任何帮助将非常感激!
import pathlib
import shutil
home = pathlib.Path.home()
pictures = home / "Pictures"
imgfiles = pictures / "imgfiles"
file1 = pictures / "image1.png"
file2 = pictures / "image2.gif"
file3 = pictures / "Uplay" / "image3.png"
file4 = pictures / "camera roll" / "image4.jpg"
filelist = [file1, file2, file3, file4]
sourcefiles = []
destination = imgfiles
for file in filelist:
file.touch()
for file in pictures.rglob("image?.???"):
sourcefiles.append(file)
for path in sourcefiles:
path.replace(destination)
回溯看起来如下:
Obtain the following Error when running the last line of code: PermissionError: [WinError 5]
Traceback (most recent call last):
File "<pyshell#42>", line 2, in <module>
path.replace(destination)
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python38\lib\pathlib.py", line 1366, in replace
self._accessor.replace(self, target)
PermissionError: [WinError 5] Acesso negado: 'C:\Users\XXXX\Pictures\image1.png' -> 'C:\Users\XXXX\Pictures\imgfiles'
您正试图将文件作为目录写入,Windows 不喜欢这样。如果您将目标替换为目标文件名,它就可以正常工作。
所以如果我要重写你的代码,我会这样做:-
import pathlib
import shutil
home = pathlib.Path.home()
pictures = home / "Pictures"
imgfiles = pictures / "imgfiles"
file1 = pictures / "image1.png"
file2 = pictures / "image2.gif"
file3 = pictures / "Uplay" / "image3.png"
file4 = pictures / "camera roll" / "image4.jpg"
filelist = [file1, file2, file3, file4]
sourcefiles = []
destination = imgfiles
for file in filelist:
file.touch()
for file in pictures.rglob("image?.???"):
destination=imgfiles/ str(file)[str(file).rfind("\")+1:] #Added
file.replace(destination)
#Commented
#for path in sourcefiles:
# path.replace(destination)
希望对您有所帮助。
替换:
for path in sourcefiles:
path.replace(destination)
与:
for path in sourcefiles:
path.replace(destination / path.name)
感谢来自 RealPython 的 Bart 的回答。