Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 - urllib.request permission denied

When I try to download a file in python 3.3.2 with the urllib.request.urlretrieve function, I get the following error:

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'

I'm trying to save the file in the dir tmp on my desktop. I'm using the following code in the module "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()

I don't know how to give python the permissions to download a file. Or is something wrong in the code?

Thank you for your help!

like image 926
Tim Woocker Avatar asked Jan 05 '14 11:01

Tim Woocker


1 Answers

I called my function with the parameter '.\\tmp' which is a directory and not a single file. You need to specify a filename for urlretrieve to work properly: '.\\tmp\\data' would be allowed as long as there is no folder called "data" inside your tmp folder.

Alternatively, since I tried to download to a temp folder, if you don't specify the second parameter, the file will automatically downloaded to your system-specific temp folder. More information can be found here: https://docs.python.org/3.7/library/urllib.request.html#urllib.request.urlretrieve

like image 169
Tim Woocker Avatar answered Nov 10 '22 03:11

Tim Woocker