本文介绍了PermissionError: [Errno 13] 权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python34lib	kinter\__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!'
                              '
And saved where you asked us to save it!!')

谁能告诉我我做错了什么?

Can someone tell me what I am doing wrong?

规格:Python 3.4.4 x86视窗 10 x64

Specs :Python 3.4.4 x86Windows 10 x64

推荐答案

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

This happens if you are trying to open a file, but your path is a folder.

这很容易误操作.

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

To defend against that, use:

import os

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

如果路径实际上是文件夹,则断言将失败.

The assertion will fail if the path is actually of a folder.

这篇关于PermissionError: [Errno 13] 权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 06:25