Recently I had an idea for a script for encrypting/decrypting files in a selected folder, in which .write() plays a major role in writing encrypted/decrypted bytes to a file, and now I would like to add tracking of the progress of this very .write() like "76%|███████ |". I know that “tqdm” exists, but from the examples on the Internet there is only downloading a file with tracking the writing of content to a file, and can someone help remake a script for tracking the speed of writing to a file of content downloaded to a file, the structure of which I unfortunately understand I can’t (pbar = () args), in a script that will track exactly .writing() in the progressbar?
tqdm example
import requests
from tqdm import tqdm
url = 'https://wordnetcode.princeton.edu/2.1/WNsnsmap-2.1.tar.gz'
filename = url.split('/')[-1]
resp = requests.get(url,stream=True)
pbar = tqdm(desc=filename, total=int(resp.headers.get('content-length', 0)),unit='B', unit_scale=True, unit_divisor=1024,)
with open(filename, 'wb') as f:
for data in resp.iter_content(chunk_size=1024):
f.write(data)
pbar.update(len(data))
pbar.close()
What i want to do
#my block of code
for filefolder, dirs, files in walk(input_dir):
for filename in files:
pbar = tqdm(desc=filename, .?.)
print(f"Encrypting {filename}..")
try:
with open(filefolder + "\\" + filename, 'rb') as encryptingfile:
b85_bytes = b85encode(encryptingfile.read())
output_file = open(filefolder + "\\" + filename, 'wb')
output_file.write(b85_bytes)
#pbar.update() equivalent, which displays the progress output_file.write(b85_bytes)
output_file.close()
except PermissionError:
print(f"\rSkipping: {filename}\n")
I was trying to replace
for data in resp.iter_content(chunk_size=1024):
to
for data in b85_value:
I have written several functions for this task for you. I hope it solves your problem
import os
import time
from tqdm import tqdm
def iter_files(folder):
for root, _, files in os.walk(folder):
for file in files:
file = os.path.join(root, file)
yield os.path.getsize(file), file
def iter_with_progress(folder):
progress = tqdm(unit='B',
total=sum(s for s, _ in iter_files(folder)),
unit_scale=True,
unit_divisor=1024)
for size, file in iter_files(folder):
done = lambda: progress.update(size)
yield done, size, file
for done, size, file in iter_with_progress('C:\Python311'):
time.sleep(1)
done() # Call done after encryption is complete