Differences
This shows you the differences between two versions of the page.
python_wiki:print_status_messages [2017/09/16 13:56] billdozor created |
python_wiki:print_status_messages [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Print Status Messages ====== | ||
- | |||
- | **General Information** | ||
- | |||
- | Printing status messages in Python scripts involves flushing the standard output buffer. | ||
- | |||
- | If the buffer is not flushed, Python will decide on its own when to flush it, resulting in sporadic large amounts of output. (Not useful for command line scripts/ | ||
- | |||
- | **Checklist** | ||
- | * Python 2 or 3 | ||
- | |||
- | ---- | ||
- | |||
- | ====== Usage ====== | ||
- | |||
- | Using the example snippet. | ||
- | <code bash> | ||
- | chmod +x print-status-msg.py | ||
- | ./ | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== The Code ====== | ||
- | |||
- | <code python print-status-msg.py> | ||
- | # | ||
- | # | ||
- | # Import Modules | ||
- | # | ||
- | # Future print function capabilities | ||
- | from __future__ import print_function | ||
- | |||
- | # sys: for stdout print buffer flush | ||
- | import sys | ||
- | |||
- | # time: for sleeping | ||
- | import time | ||
- | |||
- | #-- Example status message code --# | ||
- | print(" | ||
- | |||
- | # Loop from 0 to 59 | ||
- | for count in range(59): | ||
- | |||
- | # Print a period ' | ||
- | print(' | ||
- | |||
- | # Flush the stdout buffer to display it immediately to the console | ||
- | sys.stdout.flush() | ||
- | |||
- | # sleep for 1 second | ||
- | time.sleep(1) | ||
- | |||
- | print(" | ||
- | </ | ||
- | |||
- | ---- | ||