You are currently viewing Python Programming – Handling File IO Operations

Python Programming – Handling File IO Operations

In this article, we will see how to read data from a file and how to write data to a file in python. File IO is a very common operation with any programming language. Python offers built-in functionality to do IO operations and you don’t have to rely on any external libraries.

Python File IO – Open And Close Files In Python

To either read some data or write some data you should first open the file. There are two ways to open files in python.

The first way is to use a python built-in function called open() which can open a file for reading or writing. This open() function returns a file object using which you can do various file operations. From your python interpreter run the following command to access the documentation.

help(open)

There are two important arguments you have to pass to the open() function.

f = open("filename","mode")
  1. Filename – Absolute or Relative file path.
  2. Mode – Mode in which the file is opened. By default, it is set to read in text mode. Following are the supported mode of operation and we will see about each in their respective section.

To create a file run the following piece of code. 

f = open("nixzie_file.txt")

As foretold the open function returns a file object and you can check the object type using the type() function.

type(f)

To check the list of methods and attributes for the file object you can use the built-in dir() function.

dir(f)

To close a file run the close() method on the file object. So typically your code should look like this.

f = open("nixzie_file.txt")
<< Process the file data >>
f.close()

Alternatively, you can use the open() function with the context manager(with statement). Context manager provides better resource handling. In context manager, the opened file will be automatically closed. 

Context manager syntax is as follows. 

with open("nixzie_file.txt") as f:
    << process the file data >>

Python File IO – Write Data To Files Using Python

To write data to the file you should open the file in write(w) or append(a) mode or exclusive creation mode(x).

  1. Write Mode(w) – In write mode, a file will be created if not available, and if the file is already available it will be overwritten with the new data you write.
  2. Append Mode(a) – In append mode, a file will be created if not available and any new data you write will be appended to the file.
  3. Exclusive Creation Mode(x) – In this mode, a file will be created if not available, and if the file already exists it will throw “FileExistsError”.

Python File IO – Write & Writelines Method

To write data to a file you can either use the write() or the writelines() method.

  1. write() – This method takes a string or sequence of bytes as an argument and writes it to the file.
  2. writelines() – This method will accept a list of items and write it to the file.

In both the methods newline(\n) will not be added to the string and you have to explicitly add new lines to your strings.

First, we will see how to use the write() method and create an empty file. All you have to do is open the file either in “w”, “a”, or “x” mode. The “pass” statement in python is a null statement.

with open("nixzie_file.txt") as f:
    pass

In the below snippet, I am writing two lines to the file named “nixzie_file.txt”.

# Without Newline
with open("nixzie_file.txt","w") as nzf:
    nzf.write("My first dummy line")
    nzf.write("My second dummy line")

# With Newline
with open("nixzie_file.txt","w") as nzf:
    nzf.write("My first dummy line\n")
    nzf.write("My second dummy line\n")

You can also store the string in a variable and pass the variable name as an argument to the write() method.

var1 = """My first dummy line
My second dummy line
My Third dummy line
"""
with open("nixzie_file.txt","w") as nzf:
    nzf.write(var1)

For the writelines() method you can pass the list of items as an argument. In the below example created a list named ‘flist’ and it has three items.

flist = [ "My First dummy line\n", "My second dummy line\n", "My Third dummy line\n"]
with open("nixzie_file.txt","w") as nzf:
    nzf.writelines(flist)

If you try to write a list of items through the write() method it will fail since the write method only accepts a string as an argument. You can convert a list to a string and then use the write() method. There are many ways to convert a list to a string but here I am using the string join() method.

flist = ["My First dummy line", "My second dummy line", "My Third dummy line"]
with open("nixzie_file.txt","w") as nzf:
    nzf.write("\n".join(flist))

You can also check if the file is writable by using the writable() method. The output of the writable() method will be “True” if the file object is writable and “False” if the file object is not writable.

with open("nixzie_file.txt","r") as nzf:
    print("In read mode =>",nzf.writable())

In read mode => False

with open("nixzie_file.txt","w") as nzf:
    print("In write mode =>",nzf.writable())

In write mode => True

Redirecting Python Print Statement Output To A File

By default print() function will print the output to stdout. By default, stdout is the terminal from where you triggered the script. There are a couple of approaches to redirecting the print statement to the file but the most simple approach is to pass the filename as an argument to the print function.

# Syntax
print(str,file=filename)

with open("nixzie_file.txt","w") as nzf:
    print("After redirecting the output",file=nzf)

Python File IO – Read Operation

To read a file you should open it in ‘r’ mode which is the default operation for the open function. There are three methods supporting read operation.

  1. read(size) – This method reads the file until the end of the file is reached and returns the output as string type.
  2. readline() – This method reads a single line from the file i.e it reads the data until a new line character(\n).
  3. readlines() – This method reads the entire file and returns the output as a list data type with each line in the file as a separate item in the list.

The read() method reads the entire file and stores the output as a string. 

You can also pass the size as an argument to the read(size) method. Instead of reading the entire file, it will read the “size” number of characters alone.

The readline() method reads a single line from the file i.e until the new line character is read.

The readlines() method read the file and returns the output as a list with each line in the file as an element of the list. You can iterate through the list and write your logic on each list element.

One commonality with the read, readline, and readlines method is the newline character(\n) will also be added at the end of each line and you should write logic accordingly to remove it.

Python File IO – seek() and tell() Method

When you read the data python tracks the current position of that file object. Think of this as a pointer. You can use the tell() method to know at what position the pointer is in.

You can also use the seek() method to move this pointer to a specific position. In the below example, you can see that I am using the readline method which will read each line. I am resetting the pointer to 0 so for each readline, it is printing from the pointer zero.

Python File IO – Reading & Writing File At The Same Time

You can use r+ or a+ mode to read data from a file and write data to the same file. Here + represent opening the file for updating. 

When you open the file in r+ mode the pointer will be at position zero for you to read the data. Once you read all the data the pointer goes to the last word and you can start writing data from there.

In the below example, you can see I read and iterate the data and convert it into uppercase, and write it back to the file.

When you use a+ mode the pointer will be set after the last line. You have to use seek(0) to reset the pointer to the starting position of the file to be read.

with open("nixzie_file.txt","a+") as nzf:
    print(nzf.tell())
    nzf.seek(0)       # Reset the pointer
    data = nzf.readlines()
    for val in data:
        nzf.write(val.upper())

Wrap Up

In this article, we have seen how to use the open function along with the context manager to read a file. We have also seen how to use the read, readline, and readlines method to read the file and write, and the writeline method to write data to the file.

Leave a Reply

nineteen − 10 =