Download File From Internet

Description: Specify a URL and then a local file path to save the file to.
Tested Platform: Python 3.11
Language: Python
"""
  Downloads file from Internet

  Specify a URL and then a local file path to save the file to.

  Usage: downloadFile('https://bravosupermarkets.com/robots.txt', 'c:\\robots.txt')
"""

import urllib.request

def downloadFile(urlPath, localFilePath):
    # Open it up
    fileObj = urllib.request.urlopen(urlPath)

    # Create a local file to hold the data we get (here "wb" is for "write binary")
    localFile = open(localFilePath, "wb")
            
    # Read from the file object, write to the local object. Then close.
    localFile.write(fileObj.read())
    localFile.close()

Posted: March 21, 2023

Return to the snippets listing