【问题标题】:Python3 - urllib.request permission deniedPython3 - urllib.request 权限被拒绝
【发布时间】:2014-01-22 20:22:57
【问题描述】:

当我尝试使用 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 下载文件的权限。还是代码有问题?

感谢您的帮助!

【问题讨论】:

  • 看起来您的程序正在保存到用户限制文件夹。检查您是否有权访问“文件名”处的位置。请查看the documentation 以获得更多说明。
  • @AshishNitinPatil 我如何授予 Python 访问文件/目录的权限?我注意到另一件事:在 python 2.5 中,urllib.urlretrieve 函数一切正常。请帮忙:)
  • 我现在不知道。对不起。还有,我说的是一个“也许”
  • 检查download函数中的参数file是否为非空字符串。我认为它正在尝试写入 .\\tmp ,它是一个目录,而不是文件名,所以它不能,访问被拒绝。编辑:我检查了 Linux,如果是这种情况,你应该有一个 IOError: [Errno 21] Is a directory: tmp

标签: python python-3.x download urllib


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-01-26
    • 2021-06-17
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2018-07-19
    • 2018-02-05
    相关资源
    最近更新 更多