3

I am trying to print lines, which replace always the one before

print "\r Getting hosts %i" % int(100/count*len(lines))

This should result in Gettings hosts 0 - 100 % in one dynamic line, but it will always print a new line instead.

Saphire
  • 1,812
  • 1
  • 18
  • 34

2 Answers2

2

print implicitly add newline after the string.

To prevent that, in Python 2.x, add , at the end of the print statement.

print "\r Getting hosts %i" % int(100/count*len(lines)),

In Python 3.x, use following add end='' argument:

print("\r Getting hosts %i" % int(100/count*len(lines)), end='')

Or use sys.stdout.write (works both in Python 2.x, Python 3.x):

import sys

....

sys.stdout.write("\r Getting hosts %i" % int(100/count*len(lines)))

Standard output normally line-buffered. You may need to flush the stream each time you write.

sys.stdout.flush()

100/count*len(lines) seems strange. Maybe you mean following?

100 * count / len(lines)
falsetru
  • 357,413
  • 63
  • 732
  • 636
0

use sys.stdout.flush() to remove the line

sys.stdout.write("\r Getting hosts %i" % int(100/count*len(lines))) # or print >> sys.stdout, "\r Getting hosts %i" % int(100/count*len(lines))
sys.stdout.flush()

e.g.

for i in range(100):
    time.sleep(0.1)
    sys.stdout.write("\r Getting hosts %i" % i)
    sys.stdout.flush() 
Sar009
  • 2,166
  • 5
  • 29
  • 48