Easy way to download and upload files with Python.

Files upload / download.


The Python 'Requests' library is a neat, user-friendly HTTP module and one of the most popular packages as it's widely used for downloading and uploading files on the web. The Requests package isn’t part of Python’s standard library at least at the moment and should be installed by pip install requests.

Download and upload files with Python.



Two simple but still useful use cases below show examples of downloading MP3 file from the WEB and uploading text file on the free server.


Downloading MP3 file from Internet:



import requests

url = "https://filesamples.com/samples/audio/mp3/sample1.mp3"

req = requests.get(url)
content = req.content

with open('sample1.mp3', 'wb') as file:
  file.write(content)

print('MP3 file is downloaded !!!')

OUT: MP3 file is downloaded !!!



Uploading text file on the free web server:



import requests

url = "https://cgi-lib.berkeley.edu/ex/fup.cgi"

file = open('textfile.txt', 'rb')

req = requests.post(url, files={"upfile":file})

#You can display HTML response
# print(req.text)

print("You've uploaded a file.")

OUT: You've uploaded a file.





See also related topics: