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.
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.
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)
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()