I am trying to write a simple tool that reads files from disk, does some image processing and returns the result of the algorithm. Since a program can sometimes take some time, I like to have a progress bar, so I know where it is in the program. And since I don't like cluttering my command line, and I'm on a Unix platform, I wanted to use the "\ r" character to print the execution line on only one line.
But when I have this code, it doesn't print anything.
# Files is a list with the filenames for i, f in enumerate(files): print '\r%d / %d' % (i, len(files)),
I also tried:
print '\r', i, '/', len(files),
Now, to make sure this worked in python, I tried this:
heartbeat = 1 while True: print '\rHello, world', heartbeat, heartbeat += 1
This code works fine. What's happening? My understanding of carriage return in Linux was that it would just move the line character to the beginning, and then I could overwrite the old text that was written earlier, until I print a new line anywhere. This does not seem to be happening.
Also, is there a better way to display a progress bar on the command line than what I'm trying to do now?
python command-line carriage-return
Jonathan sternberg
source share