Python3 - urllib.request权限被拒绝

时间:2014-01-05 11:15:18

标签: python python-3.x download urllib

当我尝试使用urllib.request.urlretrieve函数在python 3.3.2中下载文件时,出现以下错误:

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'

我正在尝试将文件保存在桌面上的dir 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下载文件的权限。或者代码中出了什么问题?

感谢您的帮助!

--- --- EDIT

我注意到pyth 2.5的每一个都运行得很好!那是什么意思! 请帮忙: - )

1 个答案:

答案 0 :(得分:8)

哦,我忘记了这个问题。我解决了我很笨^^

我没有设置文件名,而是设置了foldername,我无法将文件作为文件夹下载;)