[Next entry: "MySQL Secure Installation"]
Home » Archives » November 2008 » Stay on Same Console Line with Python Output
[Previous entry: "Kyle Versus CapitalOne"]
Home » Archives » November 2008 » Stay on Same Console Line with Python Output
[Previous entry: "Kyle Versus CapitalOne"]
Stay on Same Console Line with Python Output
7 November 2008 @ 01:06 PM MST
7 November 2008 @ 01:06 PM MST
Current Music: O.A.R. - Shattered
Current Mood: Up and Down
Current Mood: Up and Down
I wanted to do this again the other day, and had to dig through my old code to figure out how I had done it, so I thought I'd put it here so I can find it more easily in the future, and so anyone can find it too.
So, say you're running through a loop and you want to output some basic information about that loop without scrolling through a million lines on the console output. You're using Python, of course, and the standard "print" function automatically adds a newline to your input (unless you end it with a comma, but you're still stuck at the end of the line).
You can write to the stdout stream instead though and be rather tricky:
sys.stdout.write("\rLoop Iteration:" + str(i) + "/" + str(max_iterations))
sys.stdout.flush()
On Linux the "\r" will return the cursor to the beginning of the line, so you can overwrite the entire line with the new information without moving to the next line on the console. The flush() command is needed so that the information you just put into the buffer gets written out (which normally happens when a newline is encountered).
Enjoy.
[This Entry]
So, say you're running through a loop and you want to output some basic information about that loop without scrolling through a million lines on the console output. You're using Python, of course, and the standard "print" function automatically adds a newline to your input (unless you end it with a comma, but you're still stuck at the end of the line).
You can write to the stdout stream instead though and be rather tricky:
sys.stdout.write("\rLoop Iteration:" + str(i) + "/" + str(max_iterations))
sys.stdout.flush()
On Linux the "\r" will return the cursor to the beginning of the line, so you can overwrite the entire line with the new information without moving to the next line on the console. The flush() command is needed so that the information you just put into the buffer gets written out (which normally happens when a newline is encountered).
Enjoy.
[This Entry]