Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abubelinha/3ab484f7e9b5ed4e49bc9014bf8a217b to your computer and use it in GitHub Desktop.
Save abubelinha/3ab484f7e9b5ed4e49bc9014bf8a217b to your computer and use it in GitHub Desktop.
Read from URL, write to file
"""
Code to write data read from a URL to a file
Based on an answer on SO:
http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python/22721
"""
import urllib2
mp3file = urllib2.urlopen("http://www.example.com/songs/mp3.mp3")
with open('test.mp3', 'wb') as output:
while True:
data = mp3file.read(4096)
if data:
output.write(data)
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment