Download a file to a specific folder with python

喜欢而已 提交于 2019-11-30 15:42:38

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/32832822/download-a-file-to-a-specific-folder-with-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!