PermissionError:[Errno 13]权限被拒绝

问题描述 投票:13回答:9

我收到这个错误:

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

windows python-3.x tkinter permission-denied
9个回答
13
投票

EDIT

I am seeing a bit of activity on my answer so I decided to improve it a bit for those with this issue still

在Windows上实现管理员execution权限基本上有三种主要方法。

  1. cmd.exe以管理员身份运行
  2. 创建使用提升的权限执行文件的快捷方式
  3. 更改python可执行文件的权限(不推荐)

1) Running cmd.exe as and admin

由于在Windows中没有sudo命令,您必须以管理员身份运行终端(cmd.exe)以达到相当于sudo的权限级别。你可以这两种方式:

  1. 手动 在cmd.exe找到C:\Windows\system32 右键单击它 选择Run as Administrator 然后它将在C:\Windows\system32目录中打开命令提示符 前往您的项目目录 运行你的程序
  2. 通过快捷键 按windows键(通常在altctrl之间)+ X。 将出现包含各种管理员任务的小弹出列表。 选择Command Prompt (Admin) 前往您的项目目录 运行你的程序

通过这样做,您作为管理员运行,所以这个问题不应该持续存在

2) Creating shortcut with elevated privileges

  1. python.exe创建快捷方式
  2. 右键单击该快捷方式,然后选择qazxsw poi
  3. 将快捷方式目标更改为Properties
  4. 单击快捷方式属性面板中的“高级”,然后单击“以管理员身份运行”选项

"C:\path_to\python.exe" C:\path_to\your_script.py"提供的delphifirst提供的答案

3) Changing the permissions on the this question executable (Not recommended)

这是一种可能性,但我强烈反对你这样做。

它只涉及找到python可执行文件并将其设置为每次都以管理员身份运行。可能并且可能会导致文件创建(它们只是管理员)或可能需要不是管理员运行的模块的问题。


7
投票

更改要保存的目录的权限,以便所有用户都具有读写权限。


3
投票

在使用Pycharm的Windows机器上发生了这种情况。

修复:右键单击PyCharm应用程序并以管理员身份运行它。


2
投票

确保首先关闭您尝试写入的文件。


2
投票

问题可能出在您要打开的文件的路径中。尝试并打印路径,看看它是否正常我有类似的问题

python

但添加此代码后:

def scrap(soup,filenm):
htm=(soup.prettify().replace("https://","")).replace("http://","")
if ".php" in filenm or ".aspx" in filenm or ".jsp" in filenm:
    filenm=filenm.split("?")[0]
    filenm=("{}.html").format(filenm)
    print("Converted a  file into html that was not compatible")

if ".aspx" in htm:
    htm=htm.replace(".aspx",".aspx.html")
    print("[process]...conversion fron aspx")
if ".jsp" in htm:
    htm=htm.replace(".jsp",".jsp.html")
    print("[process]..conversion from jsp")
if ".php" in htm:
    htm=htm.replace(".php",".php.html")
    print("[process]..conversion from php")

output=open("data/"+filenm,"w",encoding="utf-8")
output.write(htm)
output.close()
print("{} bits of data written".format(len(htm)))

它工作得很好


1
投票

这里的问题是你的用户没有正确的nofilenametxt=filenm.split('/') nofilenametxt=nofilenametxt[len(nofilenametxt)-1] if (len(nofilenametxt)==0): filenm=("{}index.html").format(filenm) 来打开文件这意味着你需要在运行该命令之前为你的python ide授予一些管理权限。

由于您是Windows用户,您只需右键单击python ide =>选择选项'以管理员身份运行'然后运行您的命令。

如果您使用命令行来运行代码,请执行相同操作以使用管理员权限打开命令提示符。


0
投票

我遇到了类似的问题。我在Windows上使用Anaconda,我按如下方式解决:1)从开始菜单中搜索“Anaconda提示”2)右键单击并选择“以管理员身份运行”3)按照安装步骤操作...

这会处理权限问题


0
投票

仔细检查并确保您尝试写入的文件未打开,或者后台的某些程序未保留此文件/数据。这对我来说是个问题。


0
投票

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

我在使用excel时遇到了上述错误,我的excel已打开,然后我关闭并尝试运行我的代码。它对我有用。

您的文件必须是开放的。关闭您的文件并再次运行代码,它应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.