Files

  • All file input output operations involve three basic steps:
    1. Open a file to create a file object
    2. Read or write information
    3. Close a file to tell your computer that you are done with that file.

Reading Files

  • To read a file, you must open it to read.

    f = open(filename)
    

    has the same effect as:

    f = open(filename, 'r')
    

    as files are opened for reading by default.

  • The variable f such as the one above is a pointer.

    • When you first open a file, f points to the beginning of the file.
    • As you read information from the file, f is advanced.
  • Two main ways to read a file:

    1. Read one line from the file, and advance the pointer f to the next line.
    line = f.readline()
    

    Be careful, the string line read this way includes the newline at the end of the line (if there is a newline).

    1. Read all the lines in the file starting with where f is currently pointing:
    lines = f.read()
    

    Lines is a string with as many lines as there is in the file from the current position, including many new lines.

  • When done with reading the current file, you can close it.

    f.close()
    

Reading all lines in a file

  • When reading a file, you often need to iterate through all the lines. This for loop achieves this:

    f = open(filename)
    for line in f:
        print line
    

    This loop does the following:

    open the file
    read each line (using f.readline()) repeatedly
    until the end of the file is reached
       for each line, execute the loop body
    close the file when finished
    
  • There is even a shorter way to do the same thing:

    for line in open(filename):
        print line.strip() ##remove the new lines
    
  • Hate Windows? Reading a file as a single string using f.read() is not recommended. However, sometimes files may not be human readable. For example, some Windows files may not have newlines (n) but carriage return (r). In this case, the above loop does not work.

    For a file that uses r for line endings, you must read the whole file and then split it.

    f = open(filename)
    content = f.read()
    lines = content.split("\r")
    for line in lines:
        print line ##note these lines do not have new lines
    

Writing files

  • When writing files, you must open the file for write or append.

    1. Opening a file for write will erase the previous contents of the file. The pointer f will point to the beginning of an emtpy file.

      f = open(filename, "w")
      
    2. Opening a file for append will keep the current contents of the file. The pointer f will point to the end of the file.

      f = open(filename, "a")
      
  • To write a string into the file, you must use the write function:

    f.write("Hello world.\n")
    

    You must manually insert the newlines into the strings. You can write a single line or a multiline string containing many new lines.

  • When finished, you must close the file. If you do not close the file you are writing in the program, you may not record the changes you made properly.

    f.close()