To download a file using Python, there are a few libraries to choose from, but the two most commonly used are urllib
and requests
. Both of these libraries allow you to send HTTP requests, including requests to download files, and then save the response to a file.
Here's an example using urllib
:
main.py127 chars7 lines
This will download the file located at the URL https://example.com/file.txt
and save it to a file called file.txt
.
Here's an example using requests
:
main.py153 chars8 lines
This will download the same file and save it to file.txt
. Note that the response content is written to a file in binary mode ('wb'
).
Both of these examples are simple ways to download a file using Python. Depending on your needs, you may need to use additional libraries or make adjustments to handle other scenarios.
gistlibby LogSnag