Download File Utility

0.3.5 · active · verified Fri Apr 17

The `download` library provides a simple, high-level function for downloading files from the internet, including optional extraction of zip archives. It's built on `urllib.request` and offers progress bars via `tqdm`. The current version is 0.3.5, and it maintains a relatively stable API with infrequent releases.

Common errors

Warnings

Install

Imports

Quickstart

The quickstart demonstrates the core functionality of downloading a file from a URL to a specified directory. It uses `download.download()` with `verbose=True` for progress and `replace=True` to ensure idempotency. The library also supports `extract=True` for automatic unzipping of archives.

import download
import os

# Define the output directory
output_dir = './my_downloads'
os.makedirs(output_dir, exist_ok=True)

# Example of a small, public file to download (e.g., a license file from GitHub)
# Using a raw GitHub URL to ensure direct file access for demonstration.
file_url = 'https://raw.githubusercontent.com/choldgraf/download/main/LICENSE'

try:
    print(f"Attempting to download {file_url} to {output_dir}")
    # Download the file with progress and replace if it exists
    download.download(
        url=file_url,
        path=output_dir,
        replace=True, # Overwrite if file exists
        verbose=True  # Show progress bar
    )
    print(f"Successfully downloaded {file_url} to {output_dir}")

    # Example of downloading and extracting a zip file (if you have a test zip URL)
    # For this example, we'll just demonstrate the call signature, as a suitable public test zip is not always guaranteed.
    # zip_file_url = 'https://github.com/choldgraf/download/archive/refs/heads/main.zip'
    # print(f"\nAttempting to download and extract a zip from {zip_file_url}")
    # download.download(
    #     url=zip_file_url,
    #     path=output_dir,
    #     extract=True,  # Extract the contents of the zip file
    #     replace=True,
    #     verbose=True
    # )
    # print(f"Successfully downloaded and extracted {zip_file_url} to {output_dir}")

except Exception as e:
    print(f"An error occurred during download: {e}")

# Optional: Clean up the created directory
# import shutil
# if os.path.exists(output_dir):
#     print(f"\nCleaning up {output_dir}")
#     shutil.rmtree(output_dir)

view raw JSON →