繁体   English   中英

Python3 - urllib.request权限被拒绝

[英]Python3 - urllib.request permission denied

当我尝试使用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下载文件的权限。 或者代码中出了什么问题?

谢谢您的帮助!

我使用参数'.\\\\tmp'调用了我的函数,该参数是一个目录而不是单个文件。 您需要指定urlretrieve的文件名才能正常工作:只要在您的tmp文件夹中没有名为“data”的文件夹,就会允许'.\\\\tmp\\\\data'

或者,由于我尝试下载到临时文件夹,如果您未指定第二个参数,该文件将自动下载到您的系统特定临时文件夹。 更多信息可以在这里找到: https//docs.python.org/3.7/library/urllib.request.html#urllib.request.urlretrieve

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM