CS21 File I/O in Python

Files and File I/O

Files

A file is a unit of persistent storage on a computer, meaning that its contents persist across re-boots of the computer on which it is stored. Your program source code is stored in a file (e.g. myprog.py), your lab assignment README is another example of a file, and even the python interpreter program is a file (it is a file that contains an executable program that can be run on the computer).

It is often useful to have programs read and write data to/from files, particularly when there are more than a few input values a program needs or the program produces a lot of results that a user may want to save.

A file is a sequence of characters. However, unlike a list or a string, characters in the file must be read or written from the file in order starting from the first one. The current position in the file is the next location where data will be read or written to in a file. When a file is first opened, the current position is at the very first character in the file. Reading or writing data from/to a file changes the current position. For example, if the file contents are:

  Hello There
  How are you?
A first read of 7 chars will return the string "Hello T", a subsequent read of 8 chars will return the string "here\nHow" (all characters, including whitespace characters like spaces, tabs, and '\n', count).

File I/O in Python

In Python, you use a file object to read from or write characters to a file. The basics of interacting with a file object:
  1. A file is first opened, by calling the open function. This function takes the file name to open, the mode to open it in (read or write), and returns a file object corresponding to the opened file.
      infile =  open("inDat.txt", "r")   # open file named inDat.txt for reading
      outfile = open("outDat.txt", "w")  # open file named outDat.txt to write
    
  2. A file is read from or written to using file methods, such as readline() or write(). When a file is first opened, it will read or write data at the very beginning of the file.
      s = infile.readline()  # read first line of file (up to and including '\n') 
      outfile.write(s)       # write s to the very beginning of outfile
    
    The next read or write picks up where the previous one left off:
      s = infile.readline()  # read the second line of file 
      outfile.write(s)       # write s at the point after the first write left off
    
    At the end of the file is a special end of file character (EOF). In Python, calls to read method functions will return an empty string when they reach the end of the file:
      # read each line of a file and print to terminal
      s = infile.readline()  
      while(s != ""):
        print(s)
        s = infile.readline()  # read the next line
    
  3. A file should be closed when you are done with it:
      infile.close()
      outfile.close()
    

Different read methods for different ways to read from a file

Examples

Open a file and write a short poem to the file

$ python
>>> outfile = open("poem.txt", "w")
>>> outfile.write("Roses are red.\n")
>>> outfile.write("Violets are blue.\n")
>>> outfile.write("Sugar is sweet,\n")
>>> outfile.write("And so are you!\n")
>>> outfile.close()
>>> 
$ cat poem.txt 
Roses are red.
Violets are blue.
Sugar is sweet,
And so are you!

Open a file and read all lines into a list

>>> infile = open("poem.txt","r")
>>> lines = infile.readlines()
>>> infile.close()
>>> print(lines)
['Roses are red.\n', 'Violets are blue.\n', 'Sugar is sweet,\n', 'And so are you!\n']

Open a file and read using a for loop

>>> infile = open("poem.txt","r")
>>> lines = []
>>> for line in infile:
...   lines.append(line.strip())
... 
>>> infile.close()
>>> print(lines)
['Roses are red.', 'Violets are blue.', 'Sugar is sweet,', 'And so are you!']

Open a file and read a line at a time using a while loop

  infile = open("poem.txt","r")
  lines = []
  s = infile.readline()    
  while(s != ""):
    lines.append(s.strip())
    s = infile.readline()  # read the next line
  infile.close()
  print(lines)
['Roses are red.', 'Violets are blue.', 'Sugar is sweet,', 'And so are you!']
This while loop is doing the exact same thing as the for loop example, just in a different way.

In that last two examples, using the for loop or using the while loop, we are able to process each line as we read it in. The str strip() method is used to strip off leading and trailing whitespace (the '\n' at the end of each line). Then the stripped line is appended to the list. The end result is similar to using readlines().

For more info on string and list methods, see our str and list methods page.