Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python stdout carriage return

Tags:

python

i am running a for loop on a given set of files..and for monitoring purpose it writes output on which files are being currently processed..

sys.stdout.write("\rProcessing.... "+ j)
sys.stdout.flush()

where j is filenames

output of script...

Processing.... /etc/sysconfig/rhn/sources0.examplewn-slepcsbals-ipv6f

problem is that the carriage return "\r" does not clear the previous filename clearly and has previous filenames on it.

any idea how i can fix this..and make it more clear

like image 619
krisdigitx Avatar asked Sep 17 '25 19:09

krisdigitx


1 Answers

Remember the written string, and write spaces first.

sys.stdout.write('\r' + (' ' * len(old_string)))
sys.stdout.write('\rProcessing... ' + new_string)
like image 57
Cat Plus Plus Avatar answered Sep 19 '25 08:09

Cat Plus Plus