Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python download file without external library

I have a very large Python script that I am using pyinstaller with to create an exe. I need to download an XML file but would like to keep the exe as small as possible as it is already getting quite large.

Is there a method within Python to get a file from a URL? I was not able to find anything without an external library

like image 287
Grady D Avatar asked Jul 05 '26 16:07

Grady D


1 Answers

You can use urllib.urlretrieve() that saves the opened page to the specified path.

Alternatively you can open the url with urllib.urlopen() and then write the read file in the binary mode:

import urllib
urllib.urlretrieve(url, destination_path) # First and short way

with open(destination_path, 'wb') as f:   # Equivalent to the first, but longer
    f.write(urllib.urlopen(url).read())
like image 100
vaultah Avatar answered Jul 22 '26 18:07

vaultah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!