Skip to content

Instantly share code, notes, and snippets.

@LexSong
Last active September 29, 2023 07:06
Show Gist options
  • Select an option

  • Save LexSong/cdfd719b9e29fb7f5065732fd1af8cbe to your computer and use it in GitHub Desktop.

Select an option

Save LexSong/cdfd719b9e29fb7f5065732fd1af8cbe to your computer and use it in GitHub Desktop.
tqdm_file_progress_bar
import io
from tqdm import tqdm
def get_file_end_position(fileobj):
current_pos = fileobj.tell()
end = fileobj.seek(0, io.SEEK_END)
fileobj.seek(current_pos)
return end
def _tqdm_file_progress_bar(iterable, fileobj, *args, **kwargs):
total = get_file_end_position(fileobj)
with tqdm(total=total, *args, **kwargs) as progress:
for x in iterable:
progress.n = fileobj.tell()
progress.update(0)
yield x
def tqdm_file_progress_bar(iterable, fileobj, *args, **kwargs):
if fileobj.seekable():
return _tqdm_file_progress_bar(iterable, fileobj, *args, **kwargs)
else:
return tqdm(iterable, *args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment