Python Programming for the Absolute Beginner, 3rd Edition
For text files to be a viable form of storage, you need to be able to get information into them. With Python, it's also a simple matter to write strings to text files. In fact, it's even easier than reading strings from text files, because there are just two basic ways to write to text files.
Introducing the Write It Program
The Write It program creates a text file with the same contents of the
Writing Strings to a Text File
Just as before, in order to use a file, I have to open it in the correct mode. So, the first thing I do in the program is open a file in write mode:
# Write It # Demonstrates writing to a text file # Michael Dawson - 4/28/03 print "Creating a text file with the write() method." text_file = open("write_it.txt", "w")
The file write_it.txt springs into existence as an empty text file just waiting for the program to write to it. If the file write_it.txt had already existed, it would have been replaced with a brand-new, empty file and all of its original contents would have been erased.
Next, I use the write() file method, which writes a string to the file:
text_file.write("Line 1\n") text_file.write("This is line 2\n") text_file.write("That makes this line 3\n")
The write() method does not automatically insert a newline character at the end of a string it writes. You have to put newlines in where you want them. If I had left the three newline characters out of the previous lines of code, the program would write one, long string to the file.
Also, you don't have to end every string you write to a file with a newline character. To achieve the same end result, I could just as easily have stuck all three of the previous strings together to form one long string, "Line 1\n This is line 2\n That makes this line 3\n", and written that string to the file with a single write() method.
Finally, I close the file:
text_file.close()
Next, just to prove that the writing worked, I read and print the entire contents of the file:
print "\nReading the newly created file." text_file = open("write_it.txt", "r") print text_file.read() text_file.close()
Writing a List of Strings to a Text File
Next, I create the same file, using the writelines() file method. Like its counter-part, readlines(), writelines() works with a list of strings. But instead of reading a text file into a list, the method writes a list of strings to a file.
The first thing I do is open the file for writing:
print "\nCreating a text file with the writelines() method." text_file = open("write_it.txt", "w")
I open the same file, write_it.txt, which means I wipe out the existing file and start with a new, empty one. Next, I create a list of strings to be written, in order, to the file:
lines = ["Line 1\n", "This is line 2\n", "That makes this line 3\n"]
Again, I inserted newline characters where I want them in the text file.
Next, I write the entire lists of strings to the file with the writelines() method:
text_file.writelines(lines)
Finally, I close the file:
text_file.close()
Lastly, I print out the contents of the file to show that the new file is exactly the same as the previous version:
print "\nReading the newly created file." text_file = open("write_it.txt", "r") print text_file.read() text_file.close() raw_input("\n\nPress the enter key to exit.")
You've seen a lot of file read and write methods. Take a look at Table 7.2 for a summary of them.
Method | Description |
---|---|
read([size]) | Reads size characters from a text file and returns them as a string. If size is not specified, the method returns all of the characters from the current position to the end of the file. |
readline([size]) | Reads size characters from the current line in a text file and returns them as a string. If size is not specified, the method returns all of the characters from the current position to the end of the line. |
readlines() | Reads all of the lines in a text file and returns them as elements in a list. |
write(output) | Writes the string output to a text file. |
writelines(output) | Writes the strings in the list output to a text file. |
Категории