我遇到了这个错误:
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)
而不是手动字符串操作。 - Łukasz Rogalskiopen(place_to_save, 'w+')
代替open(place_to_save, 'wb')
。我记得看到一些其他的SO帖子也涉及到了同样的问题。 - DevLoungeopen('/goodbye.txt', 'wb')
。如果这也会引发错误,那么tkinter
标签是无关紧要的,应该将其移除。该问题应该标记操作系统标签,因为这与问题相关。 - Terry Jan Reedyprint(directory, selected_text, place_to_save)
的输出是什么?我猜directory
由于某些原因是一个空字符串。我建议在filedialog.askdirectory
调用中添加initialdir=r'c:/'
。 - J.J. Hakalawith open(place_to_save, 'wb') as file:
,也许还有路径本身。此外,堆栈跟踪不完整。 - Gulzar