Last active
December 20, 2023 14:34
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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