Download a file to a specific folder with python
Asked Answered
B

1

7

I am trying to download a particular file to a specific folder on my hardisk. I am using IronPython 2.7 and urllib module.

I tried downloading the file with the following code:

import urllib

response = urllib.urlretrieve(someURL, 'C:/someFolder')
html = response.read()
response.close()  

But when upper code is ran, I get the following error message:

Runtime error (IOException): Access to the path 'D:\someFolder' is denied.
Traceback:
line 91, in urlretrieve, "C:\Program Files\IronPython\Lib\urllib.py"
line 9, in script
line 241, in retrieve, "C:\Program Files\IronPython\Lib\urllib.py"

I tried entering the Attributes of my "someFolder", and found out the "Read-only" is checked. I unchecked it, clicked "Apply" and "Ok". But when I come back again, "Read-only" is checked again. No matter how many times I uncheck it and confirm, it the "sameFolder" still remains "Read-only". Is this the reason why I am getting the upper error message?

Hot to fix it? I tried moving the "someFolder" to other partitions, but still for some reason I can not uncheck the "Read-only" attribute (I can, but it just keeps coming back).

Thank you for the reply.

I forgot to say that I use Windows XP 32 bit.

EDIT: I checked if "someFolder" is writable and it is. The following code:

print os.access("C:/someFolder", os.W_OK)

returns: True.

Bouley answered 28/9, 2015 at 22:30 Comment(0)
C
5

You may want to use this instead:

import os
import urllib

fullfilename = os.path.join('C:/somedir', 'test.html')
urllib.urlretrieve("http://www.google.com", fullfilename)
Corking answered 28/9, 2015 at 22:37 Comment(3)
Thank you Pedro. It worked. How can I close the response = urllib.urlretrieve(someURL, fullfilename)? When I try response.close() it prompts an error: 'tuple' object has no attribute 'close'.Bouley
I had the same error while testing your code. Honestly, I don't know.Corking
It seems that only urllib.urlopen supports close method, but not urllib.urlretrieve Is it important if I do not close the response ?Bouley

© 2022 - 2024 — McMap. All rights reserved.