[Tutor] Writing to text files

Byron byron at christianfreebies.com
Mon Aug 22 16:09:22 CEST 2005


Hi Johan,

It's actually fairly simply and straight forward...  Here's how to do 
it:  (I haven't officially tested this code for bugs, but I believe it 
is correct.)

file = open("datafile.txt", "r")
filedata = file.read()
file.close()

newLine = "Your new line of data with the time stamp goes here.\n" + 
filedata
file = open("datafile.txt", "w")
file.write(newLine)
file.close()

---

Hope this helps,

Byron  :-)
---




Johan Geldenhuys wrote:

> Hi all,
> I want to write to a text file with a timestamp, but I want to newest 
> entry at the top. So I want to insert the next entry to the file at 
> the beginning.
> I can create and append to a file and then the latest entry is at the 
> bottom.
> Any ideas how this is done please?
>
> Thanks,
>
> Johan
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>  
>




More information about the Tutor mailing list