[Tutor] Writing over a line in a text file

Bob Gailer bgailer at alum.rpi.edu
Wed Jun 21 17:58:53 CEST 2006


kieran flanagan wrote:
> Hi
>
> I have a question regarding writing text to a file. I am directing 
> output to a logfile. During one of the loops, I have a command that 
> will issue a message on how long it is waiting for by doing something 
> like this
>
> while something:
>
>                 print "\rNow waiting %s seconds .. " % seconds,
>                 sys.stdout.flush()
>                 print "\r                   ",
>
> I am trying to change my scripts so all info can be accessed via 
> logfiles. Is there any way I can direct the output to a logfile, as 
> above, without creating a new line for each print statement. I have 
> tried changing the sys.stdout to the logfile in question but the print 
> commands just force a new line for each print statement.
Writing to any file appends characters. The \r does NOT back up.

You might be able to use the tell() method of sys.stdout to determine 
the position just before the first print and the seek() method to 
reposition the file prior to each subsequent print.

See "2.3.9 File Objects" in the Library Refernce.
>
> Any ideas would be welcome.
>
> Cheers
> Kieran
>
>
> -- 
> "Behind every great man, there is a great woman. Behind that woman is 
> Mr.T."
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list