MLSC
MLSC

Reputation: 5972

sys.stdout.flush() doesn't work properly within loop in python

I have this code:

sys.stdout.write("\r [*] Serching for "+FirstName+" AND "+LastName )
sys.stdout.flush()

But when I put it in loop, step by step I have mixture of FirstNames with each other and also LastNames with each other.

Searching for TEST_THREE AND EXAMPLE_THREE   #First time
Searching for TEST_TWOEE AND EXAMPLE_TWOEE   #Next time 

You see there is EE of THREE after TWO...

How can I fix it?

Upvotes: 4

Views: 3006

Answers (1)

falsetru
falsetru

Reputation: 368894

Pad the string with extra spaces. For example, using str.ljust:

msg = "[*] Serching for {} AND {}".format(first_name, last_name)
sys.stdout.write("\r " + msg.ljust(70))
sys.stdout.flush()

using str.format:

msg = "[*] Serching for {} AND {}".format(first_name, last_name)
sys.stdout.write("\r {:<70}".format(msg))
sys.stdout.flush()

Upvotes: 5

Related Questions