【问题标题】:PermissionError: [WinError 5] when running scriptPermissionError: [WinError 5] 运行脚本时
【发布时间】:2020-06-08 12:33:12
【问题描述】:

我收到了代码 sn-p 底部注释掉的错误。 我在 IDLE 中运行所有这些。我在 Windows 上,并尝试在 IDLE 上以管理员身份右键单击运行,这可能会有所帮助,但它不起作用。我以前运行过这样的代码来完成相同的任务,但在不同的文件夹上。我也知道还有其他更简单的方法可以移动文件,但是由于我正在按照书中的练习进行操作,因此我想以尽可能接近的方式完成将多个文件移动到不同目录的最终目标我在下面描述的。具体来说,我正在寻找有关导致此错误的原因并了解原因的输入。

我看到了this 视频,从 ff 到 [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'

【问题讨论】:

    标签: python python-3.x file-io


    【解决方案1】:

    您正在尝试将文件写入目录,而 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) 
    

    希望对您有所帮助。

    【讨论】:

    • 嗨,Sherin,非常感谢您回复我。与此同时,其他人在另一个平台上回答了这个问题。我会把它贴在这里以防其他人需要它。最后一行代码应替换为以下内容:path.replace(destination / path.name)
    • @TiagoMendes:这正是我所做的,我没有使用path.name,而是使用了str(file)[str(file).rfind("\\")+1:] 。我相信您会看到变量destinationimgfiles 包含相同的值。
    • 嗨,Sherin,你是对的。我不明白这一切背后的逻辑,但在尝试了一些事情之后,他的答案是有道理的,你的也是。
    • 乐于助人!
    【解决方案2】:

    替换:

    for path in sourcefiles:
        path.replace(destination)
    

    与:

    for path in sourcefiles:
        path.replace(destination / path.name)
    

    感谢来自 RealPython 的 Bart 的回答。

    【讨论】:

      猜你喜欢
      • 2021-01-29
      • 2023-03-27
      • 1970-01-01
      • 2021-03-23
      • 2021-03-17
      • 2020-05-03
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多