在python中,sys.stdout.flush()在循环中不能正常工作

时间:2021-04-14 00:03:37

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

你看,2点过后是3点……

How can I fix it?

我怎样才能修好它?

1 个解决方案

#1


5  

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

用多余的空格填充字符串。例如,使用str.ljust:

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

using str.format:

使用str.format:

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

#1


5  

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

用多余的空格填充字符串。例如,使用str.ljust:

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

using str.format:

使用str.format:

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