Python3 - urllib.request permission denied
Asked Answered
T

1

6

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!

Tyburn answered 5/1, 2014 at 11:15 Comment(4)
Looks like your program is saving to a user restricted folder. Check whether you have permissions to access the location at the "filename". Have a look at the documentation for more clarification.Nester
@AshishNitinPatil And how do I give Python the permissions for accessing the file/directory? And I noticed another thing: In python 2.5 everything works fine with the urllib.urlretrieve function. Please help :)Tyburn
I have no clue now. Sorry. Also, what I said is a "maybe"Nester
Check whether the parameter file in the download function is not an empty string. I think it's trying to write to .\\tmp wich is a directory, not a file name, so it can't, access denied. EDIT: I checked on Linux, if this was the case, you should have a IOError: [Errno 21] Is a directory: tmpRedolent
T
13

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

Tyburn answered 29/12, 2014 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.