1

I am new to Python and I am using urllib2 to download files over the internet. I am using this code

import urllib2
response = urllib2.urlopen('http://www.example.com/myfile.zip')
...

This code actually save the zip file on my temp folder, I don't want it to be like that, I want to save it on my desired location. Is it possible?

kagat-kagat
  • 1,455
  • 4
  • 22
  • 27

2 Answers2

4

You can use the urllib.urlretrieve function, to download the distant file to your local filesystem.

>>> import urllib
>>> urllib.urlretrieve('http://www.example.com/myfile.zip', 'path/to/download/dir/myfile.zip')

See the urllib.urlretrieve documentation for more info.

Balthazar Rouberol
  • 6,822
  • 2
  • 35
  • 41
1

Simply use something like this :

f = open("path_to_your_file_to_save", 'w')
f.write(urllib.urlopen(url).read())
f.close()

Where path_to_your_file_to_save equals [path_where_save] + [filename.ext]

rphonika
  • 1,071
  • 10
  • 11