[Tutor] Reuse output line on stdout

alan.gauld@bt.com alan.gauld@bt.com
Wed, 17 Oct 2001 12:15:44 +0100


> Total Python newbie here.  I have a loop that reads
> and processes lines in a file.  I want some feedback
> so I have my script print the line number under
> process to stdout.  

You mean something like:

000001: Here's some output
000003: Here's some for line 3
000012: Next line was 12
008732: Didn't do much with the previous stuff
009856: This will be the last line

> several hundred thousand lines how do I get Python to
> reuse the same line on stdout, i.e. I don't want a
> newline/linefeed at the end of the output line, 

So instead of a sequence like above you only want 
a single line printed? Given the speed of processing
you might be best to just print the last line!
As a progress indicator maybe print a line of dots, 
something like:

print "Processing file"
linecount = 0
for line in f.readlines():
   linecount += 1
   if linecount%80 != 0 : print '.', # no newline
   else: print '.' # newline every 80 lines
   # do your processing here, including formnatting the
   # last line message
print "\n %s" % message

That will yield:

Processing file: spam.txt
..........................................................
.........................................
143 : Did something on line

Of course if it's thousands of lines long you might only want 
to print a dot every 50 lines or whatever scaling fdactor 
seems suitable - the idea is just some visible progress
feedback to the user.

I hope that helps, I'm not absolutely sure I 
understood the intent.

Alan G