Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Download Files using Python?

HI, everyone. I am new to Python and am using Python 2.5 on CentOS.

I need to download files like WGET do.

I have done some search, and there are some solutions, an obvious way is this:

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

This works fine. But I want to know, if the mp3 file is VERY large, like 1Gb, 2Gb or even bigger. Can this code snippet still work? Are there better ways to download large files in Python, maybe with a progress bar like WGET do.

Thanks a lot!

like image 218
DocWiki Avatar asked Nov 30 '22 05:11

DocWiki


1 Answers

There's an easier way:

import urllib
urllib.urlretrieve("http://www.example.com/songs/mp3.mp3", "/home/download/mp3.mp3")
like image 62
Paul Schreiber Avatar answered Dec 05 '22 03:12

Paul Schreiber