python - 权限错误 : [Errno 13] Permission denied

标签 python python-3.x windows tkinter permissions

我收到这个错误:

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

最佳答案

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

这很容易因错误而发生。

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

import os

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

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

关于python - 权限错误 : [Errno 13] Permission denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36434764/

相关文章:

python - 如何修复 QPropertyAnimation "starting an animation without end value"错误

python - 从 Flask 框架编写 CSV

python - 如何保存输入的文本? (Python)

python - 为什么不切换到 Python 3.x?

windows - 写入系统驱动器 C : without admin rights in Delphi 上的文件

python - python可以将文本发送到Mac剪贴板吗

python - 按第二个值(或按总和)对 "Tuple"进行排序

python - window Python 3.6.4 : Can't find IDLE

windows - 具有所有 Windows 依赖项的 couchDB 完整包

Windows 应用程序 - DPI 问题