[Tutor] creating text files
Stanfield, Vicki {D167~Indianapolis}
vicki.stanfield at ROCHE.COM
Thu Dec 11 11:59:06 EST 2003
A follow-up question if you don't mind....
I open a log file while my program is running, but sometimes the program
crashes and leaves no log file. Is there any way to write the file prior
to when I actually close it. I guess I could close it and reopen it, but
I'd rather just flush the output to the file.
--vicki
-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Alan Gauld
Sent: Thursday, December 11, 2003 11:55 AM
To: ali; tutor at python.org
Subject: Re: [Tutor] creating text files
> am a newbie and was wondering how to create a new text file in
python...
Check out the files and text page in my tutorial.
Basically you open it with
myfile = open("foo.txt", "r") # to read
myfile2 = open("foo2.txt", "w") # to create a new file for writing
myfile3 = open("foo3.txt","a") # to write at the end of an existing
file
IN more odern versions of Python (v2.x?) you can substitute
file() for open():
myfile = file("foo.txt","r")
etc...
The tutor explains how to read and write data with those files.
Finally its a good idea to close the file when done:
myfile.close()
HTH
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list