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


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 =
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

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")
The write() function takes a string as input parameter.

Reading and Writing to Stdin and Stdout

Sometimes you do not know the name of the input file or output file when you are writing your program. You can then read from stdin and write to stdout. The names of the input file and output file are supplied to the program at run time, like so:

python3 < input.txt > output.txt

Here is how you take care of reading in lines. You do not have to do anything special for writing to the output file.

import sys

# to read just one line of data
one_line = sys.stdin.readline()

# to read all the lines of data as a single string
all_lines = sys.stdin.readlines()

# to read all the lines of data one line at a time
for line in sys.stdin:
  # process line of data

# to read only n lines of data one line at a time
for i in range (n):
  line = sys.stdin.readline()