简体   繁体   English

使用python将文件下载到特定文件夹

[英]Download a file to a specific folder with python

I am trying to download a particular file to a specific folder on my hardisk. 我正在尝试将特定文件下载到我的硬盘上的特定文件夹中。 I am using IronPython 2.7 and urllib module. 我正在使用IronPython 2.7和urllib模块。

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. 我尝试输入我的“someFolder”的属性,发现“只读”已被检查。 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". 无论我多少次取消选中并确认,“sameFolder”仍然是“只读”。 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). 我尝试将“someFolder”移动到其他分区,但仍然由于某种原因我无法取消选中“只读”属性(我可以,但它只是不断回来)。

Thank you for the reply. 感谢您的答复。

I forgot to say that I use Windows XP 32 bit. 我忘了说我使用Windows XP 32位。

EDIT: I checked if "someFolder" is writable and it is. 编辑:我检查了“someFolder”是否可写,确实如此。 The following code: 以下代码:

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

returns: True . 返回: True

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM