How to download a file using Python

To download a file using Python you can use the snippet below.

Sample Python

import urllib.request
import shutil

# file_name = target file name
with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
    shutil.copyfileobj(response, out_file)

Please rate this snippet

How to check if a variable exists in Python

To check if a variable exists in Python, you can use the snippet below.

Sample Python

if 'myVariable' in locals(): # myVariable exists locally.
if 'myVariable' in globals(): # myVariable exists globally.

Please rate this snippet