Download file

We can download data using the urllib2 module.. These examples work with both http, https and for any type of files including text and image.

Data will be saved in the same directory as your program resides. This means if you run your program from C:\apps\, images will be saved there too unless you explicitly specify another directory.

Related Courses

Download data to variable
To download data to a variable, you can use this code:

The first line, ‘import urllib2’, loads the module. The second line opens the connection to the url. The method downloads data from the url and stores it into the variable data.

Download text file
To download a file you can use this code:

Download image file
Downloading an image from the web works in the same way:

Download network examples

Previous Post
Next Post


  • Khoa says:

    Hi, is this the end of the tutorial? Great thanks!

  • Boycha says:

    While downloading the text file decode it in utf-8 or else it will give an error because the argument of file_.write needs to be string

    • Wilson says:

      I just find the solution
      file = open(“readddme.txt”,”wb”)

Leave a Reply