-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to create a dynamic progress bar, which increases its maximum size while looping? #735
Comments
not sure exactly what you want here - could you provide example desired output? |
For example:
On this example, when new files are found, the maximum and initial limit was increased from 15 to 60 (because more 45 files where found). And later on, when more new files were found, the maximum limit was increased from 60 to 150 (because more 90 files were found). on the end, no more new files were found, and everything was parsed. |
I presume this increase in
but I was more concerned about how you increase
Your original code also had some issues, e.g one of your You should really check out https://github.com/tqdm/tqdm/wiki/How-to-make-a-great-Progress-Bar But for your specific example... from tqdm import tqdm
import os.path
def find_files_recursively(sourcepath, show_progress=True):
files = []
# total=1 assumes sourcepath is a file
with tqdm(total=1, desc="Searching", unit="file", disable=not show_progress) as myprogressbar:
def append_found_file(fullpath):
files.append(fullpath)
#from time import sleep; sleep(0.1) # for testing in small dirs
myprogressbar.update()
def list_found_dir(path):
"""returns os.listdir(path) assuming os.path.isdir(path)"""
listing = os.listdir(path)
# we subtract 1 since a "file" we found was actually this "path" (a directory)
myprogressbar.total += len(listing) - 1
myprogressbar.set_postfix(dir=path[-7:]) # fancy way to trigger refresh & give info
# myprogressbar.update(0) # alternative way to trigger a refresh without info
return listing
if os.path.isdir(sourcepath):
def recursively_search(sourcepath):
filenames = list_found_dir(sourcepath)
for somefile in filenames:
fullpath = os.path.join(sourcepath, somefile)
if os.path.isdir(fullpath):
recursively_search(fullpath)
else:
append_found_file(fullpath)
recursively_search(sourcepath)
elif os.path.exists(sourcepath):
append_found_file(sourcepath)
return files I'm going to add this to the documentation as a nice example I think. |
I got this recursive function which given a directory, recursively finds all files on it:
How could I write a progress bar which updates while new files are being found?
I should update each time I list files in a directory, for example:
The text was updated successfully, but these errors were encountered: