Using files in Python

Files
Files are persistent data storage. When you run a program, its memory (stack and values space) is lost when the program exits. When you close a file, its contents persist. For example, when you exit vim after making some changes to a Python program in a file named prog.py, those changes persist between instance of opening the file in vim.

Often times programs uses files to get initial input values and to write out results before exiting. This way starting up a program can be fast (the user doesn't have to enter all the input by hand), and the effects of a program can persist (we can keep around the results of a program run in a file. One program can be used to create as its output an input file for another program (further easing the user's having to create a huge input file).

One example of a type of program that use files data is a database program. A company keeps information about Employees in a file that are read in by a database program when employee information needs to be accessed or updated. The database program will read in the database file on start-up and write any modifications to the database file before it exits. For example, if an employee gets a raise, the database program reads in the employee file, finds the employee's information from the file, updates it, and writes the resulting change in the employee's salary to the employee file so that it is not lost when the database program exits.

Steps for Using files in Python Programs
To read or write data from files, you need to follow these 3 steps:
  1. open the file: call the open function passing it two strings. The first string is the name of the file (the Unix path name of the file), the second is a string representing the mode in which you want to open the file: "r" to read the file; "w" to write to the file; "a" to append to a file; "w+" to open it for both reading and writing.

    Here are some example calls to open:

    # open foo.txt for reading (the file's relative path name is used in this example)                           
    infile = open("foo.txt", "r")  
    
    # open blah.txt for writing (the file's absolute path name is used in this example)
    outfile = open("/home/newhall/blah.txt", "w")  
    	
    If the file is successfully opened, open returns a file object associated with the open file.

  2. read or write from the file. When reading or writing from a file, there is a current position from which reads and writes read or write from. When a file is first opened, the current position is at the first character in the file. If you then read one character from the file, the current position moves to the second character in the file. A subsequent read will read the second character and advance the current position.

    To read or write to a file, you need to call methods of the file object returned by the call to open. To list file documentation, in the python interpreter:

     
      $ python 
      >>> help(file) 
    

    Here are a few read methods that may be useful:

      ch = infile.read(1) # read one character from the file 
                          # returns: a single char string 
                          #          or "" if there are no more chars to read 
                          # curr position: advanced to next char in file
    
      line = infile.readline()  # read all the characters from the current pos
                                # to the end of the current line (up to and 
                                # including the eoln character '\n')
                                # returns: a string containing the next line 
                                #          or "" if there are no more lines to read 
                                # curr position: first char on the next line
    
      lines = infile.readlines()  # read in every line of the file up to the end 
                                  # returns: a list of strings, one string
                                  #          per line in the file (each string
                                  #          includes the eoln char at the end)
                                  # curr position: at the end of the file 
    
    For example, if the file foo.txt contains:
      hello
      there
      hi
    
    Then:
      infile = open("foo.txt", "r")
      lines = infile.readlines()
      infile.close()
    
    Reads in the entire contents of the file; readlines returns a list of three strings, one for each line in the file. The variable lines is a list containing (notice the \n char at the end of each string):
      [ "hello\n", "there\n", "hi\n" ]
    

  3. close the file when done reading or writing from it:
      infile.close()
    
Examples
With the week07 in-class files, is a file filetest.py that contains examples of different ways to read data from a file.