Non-Programmer's Tutorial for Python 2.6/File I/O

Here is a simple example of file I/O (input/output):

The output and the contents of the file  are:

This Text is going to out file Look at it and see!

Notice that it wrote a file called  in the directory that you ran the program from. The  in the string tells Python to put a newline where it is.

A overview of file I/O is:
 * Get a file object with the  function.
 * Read or write to the file object (depending on how it was opened)
 * Close it

The first step is to get a file object. The way to do this is to use the  function. The format is   where   is the variable to put the file object,   is a string with the filename, and   is   to read a file or   to write a file (and a few others we will skip here). Next the file objects functions can be called. The two most common functions are  and. The  function adds a string to the end of the file. The  function reads the next thing in the file and returns it as a string. If no argument is given it will return the whole file (as done in the example).

Now here is a new version of the phone numbers program that we made earlier:

Notice that it now includes saving and loading files. Here is some output of my running it twice:

1. Print Phone Numbers 2. Add a Phone Number 3. Remove a Phone Number 4. Lookup a Phone Number 5. Load numbers 6. Save numbers 7. Quit Type in a number (1-7): 2 Add Name and Number Name: Jill Number: 1234 Type in a number (1-7): 2 Add Name and Number Name: Fred Number: 4321 Type in a number (1-7): 1 Telephone Numbers: Name: Jill    Number: 1234 Name: Fred    Number: 4321 Type in a number (1-7): 6 Filename to save: numbers.txt Type in a number (1-7): 7 Goodbye

1. Print Phone Numbers 2. Add a Phone Number 3. Remove a Phone Number 4. Lookup a Phone Number 5. Load numbers 6. Save numbers 7. Quit Type in a number (1-7): 5 Filename to load: numbers.txt Type in a number (1-7): 1 Telephone Numbers: Name: Jill    Number: 1234 Name: Fred    Number: 4321 Type in a number (1-7): 7 Goodbye

The new portions of this program are:

First we will look at the save portion of the program. First it creates a file object with the command. Next it goes through and creates a line for each of the phone numbers with the command. This writes out a line that contains the name, a comma, the number and follows it by a newline.

The loading portion is a little more complicated. It starts by getting a file object. Then it uses a  loop to keep looping until a   statement is encountered. Next it gets a line with the line. The  function will return a empty string when the end of the file is reached. The  statement checks for this and  s out of the   loop when that happens. Of course if the  function did not return the newline at the end of the line there would be no way to tell if an empty string was an empty line or the end of the file so the newline is left in what   returns. Hence we have to get rid of the newline. The line  does this for us by dropping the last character. Next the line  splits the line at the comma into a name and a number. This is then added to the  dictionary.

Exercises
Now modify the grades program from section ../Dictionaries/ so that it uses file I/O to keep a record of the students.