2010-12-09 87 views
4

嗨,大家好。我是Python新手,在CentOS上使用Python 2.5。如何使用Python下载文件?

我需要下载像WGET这样的文件。

我已经做了一些搜索,并有一些解决方案,一个显而易见的方法是这样的:

import urllib2 
mp3file = urllib2.urlopen("http://www.example.com/songs/mp3.mp3") 
output = open('test.mp3','wb') 
output.write(mp3file.read()) 
output.close() 

这工作得很好。但是我想知道,如果mp3文件非常大,比如1Gb,2Gb甚至更大。这段代码片段仍然可以工作吗?有更好的方法来下载Python中的大文件,也许有像WGET这样的进度条。

非常感谢!

+0

我想你的问题是关于反复读,同时写一大块,而不是整个文件读入内存在一次只给它的所有写出来的之后的磁盘。 – chrisaycock 2010-12-09 21:31:10

+3

可能的重复[流大二进制文件与urllib2文件](http://stackoverflow.com/questions/1517616/stream-large-binary-files-with-urllib2-to-file) – katrielalex 2010-12-09 21:31:27

回答

15

有一个简单的方法:

import urllib 
urllib.urlretrieve("http://www.example.com/songs/mp3.mp3", "/home/download/mp3.mp3") 
2

为什么不直接致电wget呢?

import os 
os.system ("wget http://www.example.com/songs/mp3.mp3") 
3

对于真正的大文件,由于您将整个文件一次加载到内存中,您的代码会占用大量内存。可能会更好地读取和写入数据块:

from __future__ import with_statement 
import urllib2 
mp3file = urllib2.urlopen("http://www.example.com/songs/mp3.mp3") 
with open('test.mp3','wb') as output: 
    while True: 
     buf = mp3file.read(65536) 
     if not buf: 
      break 
     output.write(buf) 
1

您的当前代码会在写入磁盘之前将整个流读入内存。因此,对于文件大于可用内存的情况,您将遇到问题。

要解决此问题,您可以一次读取块并将它们写入文件。


(从Stream large binary files with urllib2 to file复制)

req = urllib2.urlopen(url) 
CHUNK = 16 * 1024 
with open(file, 'wb') as fp: 
    while True: 
    chunk = req.read(CHUNK) 
    if not chunk: break 
    fp.write(chunk) 

“试验了一下各种块大小,找到 ”甜蜜点“ 满足您的要求。”