当我尝试使用urllib.request.urlretrieve函数在Python 3.3.2中下载文件时,出现以下错误:
我正在尝试将文件保存在桌面上的tmp目录中。我在模块"downloader.py"中使用以下代码:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "C:\Python33\lib\site-packages\downloader.py", line 17, in startdownload
urllib.request.urlretrieve(url, file, reporthook)
File "C:\Python33\lib\urllib\request.py", line 191, in urlretrieve
tfp = open(filename, 'wb')
PermissionError: [Errno 13] Permission denied: '.\\tmp'
我正在尝试将文件保存在桌面上的tmp目录中。我在模块"downloader.py"中使用以下代码:
def download(url, file):
import urllib.request, tkinter, os, time
from tkinter import ttk
def reporthook(blocknum, blocksize, totalsize):
readsofar = blocknum*blocksize
percent = readsofar * 1e2 / totalsize
GUI.title(str(int(percent)) + "% done")
PROGRESS["value"] = percent
PROGRESS.update()
def startdownload():
BUTTON.destroy()
for y in range(70, 40, -1):
time.sleep(0.1)
GUI.geometry("500x"+str(y))
GUI.update()
urllib.request.urlretrieve(url, file, reporthook)
GUI.destroy()
GUI = tkinter.Tk()
GUI.resizable(0,0)
GUI.title("Click the download button to start downloading!")
GUI.geometry("500x70")
PROGRESS = ttk.Progressbar(GUI, length=480)
PROGRESS.place(x=10, y=10)
BUTTON = ttk.Button(GUI, text="start download", command=startdownload)
BUTTON.place(x=200, y=40)
GUI.mainloop()
我不知道如何给Python下载文件的权限。还是代码存在问题?
感谢您的帮助!
download
函数中的参数file
是否为空字符串。我认为它试图写入一个目录而不是文件名,因此无法访问.\\tmp
。编辑:我在Linux上进行了检查,如果是这种情况,你应该会得到一个IOError: [Errno 21] Is a directory: tmp
的错误提示。 - user2375821