If you are reading from a very large file, try this approach:
from tqdm import tqdm
import os
file_size = os.path.getsize(filename)
lines_read= []
pbar = tqdm.tqdm(total=file_zize, unit="MB")
with open(filename, 'r', encoding='UTF-8') as file:
while (line := file.readline()):
lines_read.append(line)
pbar.update(s.getsizeof(line)-sys.getsizeof('\n'))
pbar.close()
I left out the processing you might want to do before the append(line)
EDIT:
I changed len(line)
to s.getsizeof(line)-sys.getsizeof('\n')
as len(line)
is not an accurate representation of how many bytes were actually read (see other posts about this). But even this is not 100% accurate as sys.getsizeof(line) is not the real length of bytes read but it's a "close enough" hack if the file is very large.
I did try using f.tell() instead and subtracting a file pos delta in the while loop but f.tell with non-binary files is very slow in Python 3.8.10.
As per the link below, I also tried using f.tell() with Python 3.10 but that is still very slow.
If anyone has a better strategy, please feel free to edit this answer but please provide some performance numbers before you do the edit. Remember that counting the # of lines prior to doing the loop is not acceptable for very large files and defeats the purpose of showing a progress bar altogether (try a 30Gb file with 300 million lines for example)
Why f.tell() is slow in Python when reading a file in non-binary mode
https://bugs.python.org/issue11114