[[python_wiki:print_status_messages]]

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/programs)

Checklist

  • Python 2 or 3

Usage

Using the code snippet.

chmod +x print-status-msg.py
./print-status-msg.py

The Code

print-status-msg.py
#!/usr/bin/python
#=======================
# 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("Doing some work", end='')
 
# Loop from 0 to 59
for count in range(59):
 
  # Print a period '.' with no newline
  print('.', end='')
 
  # Flush the stdout buffer to display it immediately to the console
  sys.stdout.flush()
 
  # sleep for 1 second
  time.sleep(1)
 
print("[COMPLETE]")

  • python_wiki/print_status_messages.txt
  • Last modified: 2019/05/26 03:50
  • (external edit)