[Tutor] Writing over a line in a text file

kieran flanagan kieran.flanagan at gmail.com
Wed Jun 21 15:09:53 CEST 2006

Thanks for both your replies

Yes I think you may have misunderstood my orginal post Luke. The portion of
code I gave will ensure each time this line is written it just writes over
the previous line ( when printed to the console and  \r is not windows
specific). As I said, I have tried numerous ways to write these commands to
a logfile so the line will just be written over ( the method you gave will
just create a newline per iteration of the loop), as it is if I direct the
output to the console.
The section of code I gave is contained within a while loop, which is
constantly checking for something to appear on a website. It simple prints
out a line, letting the user know how long it has been waiting for. On the
console this will just appear as one line ( each iteration writing the line
over the previous one). In the logfile, this wil be a new line for each
iteration of the while loop.

To answer Kents questions, I previously had these outputs going to a logfile
which could just be tailed. I now want to change the way I display the logs
created. I firstly create a logfile within an apache server, this logfile
appears as a link on a frontend website beside a corresponding test name. I
then want to output all log messages to this one file. For what I am doing,
it would be extremely useful for any number of users to just click on a link
and have access to what is currently being run on the backend, in realtime.
Using both logging and plain write messages, the output provides information
on what function, script etc is running. There is one very large script,
that runs a large number of other scripts to test and change data on a

So my problem is I want to be able to have this one logfile accessible from
the web and not have a huge amount of one line messages appearing ( within
the various while loops), which just let the user know how long the script
has been waiting.

Kent, I will try your method and I hope this explains my objective a little


On 6/21/06, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
> 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.
> I don't understand what the commas atthe end of your print commands are
> for.
> Nor do I understand what you're trying to do.
> Are you trying to redirect stdout to a file?
> why bother doing this?
> why not just do
> from time import sleep
> from random import randint
> f = file("log.txt","a")
> while x < y:
>     f.write("\nNow waiting %s seconds\n" % randint(0,4))
> f.close()
> Or do I misunderstand what your objective is?
> Also, I believe \r is windows-only, and you should use \n all the time,
> or \r\n.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

"Behind every great man, there is a great woman. Behind that woman is Mr.T."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060621/afc009b6/attachment.html 

More information about the Tutor mailing list