【发布时间】:2016-07-25 21:21:44
【问题描述】:
我收到此错误:
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
【问题讨论】:
-
不应该
place_to_save只是goodbye.txt?我不确定 Windows 的行为如何,但在 Linux 上,您将写入根目录 (/),这始终是个坏主意。您应该使用os.path.join(directory, selected_text),而不是手动字符串操作。 -
尝试
open(place_to_save, 'w+')而不是open(place_to_save, 'wb')。我记得看到其他一些关于同一问题的 SO 帖子, -
一个 MCVE *.com/help/mcve 应该是一行:
open('/goodbye.txt', 'wb')。如果这也引发了,那么tkinter是无关紧要的,应该作为标签删除。这应该用操作系统标记,因为这是相关的。 -
print(directory, selected_text, place_to_save)的输出是什么?我的猜测是directory出于某种原因是一个空字符串。我会尝试将initialdir=r'c:/'添加到filedialog.askdirectory通话中。 -
@Mixone 我认为这是因为代码不是最少的。唯一相关的行是 ` with open(place_to_save, 'wb') as file:` 和 也许 路径本身。堆栈跟踪也未满。
标签: python python-3.x windows tkinter permission-denied