Skip to content

Instantly share code, notes, and snippets.

@engividal
Last active December 20, 2023 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save engividal/0a8b966f61658f34304d38d9af0f65d1 to your computer and use it in GitHub Desktop.
Save engividal/0a8b966f61658f34304d38d9af0f65d1 to your computer and use it in GitHub Desktop.
The download script is a Python program that helps you estimate the time it will take to download a file of a certain size. It does so by monitoring the current size of the downloaded file and updating a progress bar accordingly. The progress bar is displayed in the command-line interface using the tqdm library. To use the script, you need to pr…
import argparse
import time
import subprocess
from tqdm import tqdm
parser = argparse.ArgumentParser(description='Download script.')
parser.add_argument('path', type=str, help='Path to the file to be downloaded.')
parser.add_argument('size', type=float, help='Size of the file to be downloaded in GB.')
args = parser.parse_args()
total_size = int(args.size * 1024 * 1024 * 1024) # Total download size in bytes
block_size = 8192 # Download block size in bytes
start_time = time.time()
with tqdm(total=total_size, unit='B', unit_scale=True, unit_divisor=1024, desc='Downloading...') as pbar:
while True:
try:
output = subprocess.check_output(f"du -sk {args.path}", shell=True).decode("utf-8")
current_size = int(output.split()[0]) * 1024 # Convert to bytes
except subprocess.CalledProcessError:
current_size = 0
pbar.update(current_size - pbar.n)
if current_size >= total_size:
break
time.sleep(1)
elapsed_time = time.time() - start_time
print(f"Total download time: {elapsed_time:.2f} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment