I want to write the text (which I get from AJAX) to a file, and then re开发者_开发百科ad it.
If you can use this in Django view... try somethink like this:
def some_view(request):
text = request.POST.get("text", None)
if text is not None:
f = open( 'some_file.txt', 'w+')
f.write(text)
f.close()
return HttpResponse()
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()
f = open( 'filename.txt', 'w+' )
f.write( 'text' )
f.close()
f = open( 'filename.txt', 'r' )
for line in f:
print( line )
f.close()
You can try this out
with open("file.txt", "r+") as file:
for i in file:
file.write(i + "\n")
file.close
精彩评论