Writing to a text file : Text file : File PYTHON TUTORIALS


PYTHON TUTORIALS » File » Text file »

 

Writing to a text file


print "Creating a text file with the write() method."
text_file = open("write_it.txt""w")
text_file.write("Line 1n")
text_file.write("line 2n")
text_file.write("line 3n")
text_file.close()

print "nReading the newly created file."
text_file = open("write_it.txt""r")
print text_file.read()
text_file.close()

print "nCreating a text file with the writelines() method."
text_file = open("write_it.txt""w")
lines = ["Line 1n",
         "line 2n",
         "line 3n"]
text_file.writelines(lines)
text_file.close()

print "nReading the newly created file."
text_file = open("write_it.txt""r")
print text_file.read()
text_file.close()

raw_input("nnPress the enter key to exit.")



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo File
» Text file