PermissionError: \[Errno 13\] 权限被拒绝

新手上路,请多包涵

我收到此错误:

 Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter__init__.py", line 1538, in __call__
return self.func(*args)
File "C:/Users/Marc/Documents/Programmation/Python/Llamachat/Llamachat/Llamachat.py", line 32, in download
with open(place_to_save, 'wb') as file:
PermissionError: [Errno 13] Permission denied: '/goodbye.txt'

运行时:

 def download():
    # get selected line index
    index = films_list.curselection()[0]
    # get the line's text
    selected_text = films_list.get(index)
    directory = filedialog.askdirectory(parent=root,
                                        title="Choose where to save your movie")
    place_to_save = directory + '/' + selected_text
    print(directory, selected_text, place_to_save)
    with open(place_to_save, 'wb') as file:
        connect.retrbinary('RETR ' + selected_text, file.write)
    tk.messagebox.showwarning('File downloaded',
                              'Your movie has been successfully downloaded!'
                              '\nAnd saved where you asked us to save it!!')

有人可以告诉我我做错了什么吗?

规格:Python 3.4.4 x86 Windows 10 x64

原文由 Marc Schmitt 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 793
1 个回答

如果 您尝试打开一个文件,但您的路径是一个文件夹, 就会发生这种情况。

这很容易错误地发生。

为了防止这种情况,请使用:

 import os

path = r"my/path/to/file.txt"
assert os.path.isfile(path)
with open(path, "r") as f:
    pass

如果路径实际上是一个文件夹,断言将失败。

原文由 Gulzar 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题