I want to write the text (which I get from AJAX) to a file, and then read it.
-
The tutorial <a href="docs.python.org/tutorial/… this</a>.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2010-03-22 09:51:35 +00:00Commented Mar 22, 2010 at 9:51
-
Any reason, why you want to re-read it again after fetching? (Like, you want to append new data and read the whole file...)Boldewyn– Boldewyn2010-03-22 09:54:24 +00:00Commented Mar 22, 2010 at 9:54
-
@Boldewyn: Presumably to send it back out again at a later time. Appending doesn't require reading what's already in the file.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2010-03-22 10:00:24 +00:00Commented Mar 22, 2010 at 10:00
-
Hello everybody, please my English is not so good. I want to write the text (which I get from AJAX) to a file,i found this example in Ineternet ,and it is interressant ,but i don't no how i can test that.Please can u tell me witch Labrary i want bevor it works ? or can u give me some example how i can do that ,i will very happy. I thank U in advance Denisuser602421– user6024212011-02-04 01:01:23 +00:00Commented Feb 4, 2011 at 1:01
Add a comment
|
4 Answers
The following code for read the content from a file
handle=open('file','r+')
var=handle.read()
print var
If you want to read a single line use the readline(). If you want to read the whole lines in the file use the readlines() also
The following code for writing the content to the file
handle1=open('file.txt','r+')
handle1.write("I AM NEW FILE")
handle1.close()
1 Comment
Fydo
python doesn't terminate in semi-colons.
You can try this out
with open("file.txt", "r+") as file:
for i in file:
file.write(i + "\n")
file.close
1 Comment
robert
you don't need close if you're using with