Files

Opening / Closing Files

It is relatively simple to to do file manipulation in Python. To process a file it has to be opened. Python offers 3 modes for opening a file - read ("r"), write ("w"), and append ("a"). The syntax for opening a file is as follows:

  fileVar = open ("fileName", "mode")

For example,

  inFile = open ("input.txt", "r")

To close a file simply do

  inFile.close()

To read a file, the file must not only exist but also have read permissions. Whereas, if the file does not exist in the write mode a new file with that name is created. If you open a file in write mode and there is already an existing file then the original contents get over-written. If you wish to preserve the original content and still write to the file then open it in append mode. Files must be closed at the end of the operation.

Reading a Text File

You have several options in reading a file. If the file is small then it can be read in as a single string. The command to do so is read().

  infile = open ("input.txt", "r")
  fileContent = infile.read()
However, if the file is too large to fit in memory, then you will have to read the file line by line. The command to do so is readline(). This will read in all characters including the terminating newline character. To read remaining lines in the file you can use the command readlines(). The simplest way to read a file line by line would be to:
  infile = open ("input.txt", "r")
  for line in infile:
    #  process the line
    line = line.rstrip("\n")   # strip the newline character at the end
    ...
  infile.close()

Writing / Appending to a Text File

To write to a file, the files must be opened for writing. If it is opened in "w" mode then the previous content will be overwritten. If it is opened in "a" mode then the new content will added to the end of the previous content. Python provides the command write() to write or append to a file. You must explicitly add the newline character (\n) to the end of each line.

  outfile = open ("output.txt", "w")
  outfile.write ("This is a line. \n")
  outfile.close()
The write() function takes a string as input parameter.