CommonLounge Archive

Python3: File Input and Output

May 23, 2019

In projects, you are often required to read data from a file or write data to a file. Python provides inbuilt functions for handling read/write operations on files. In this tutorial, you’ll learn how to read/write a file. Let’s get started.

Opening a file

Before you read or write a file, you’ll need to open a file first. The in-built method open() is used to open a file. It will create a file object which will provide you methods to read or write a file.

Let’s look at the syntax of open() method.

file_object = open( r'file_name', 'access_mode')

As you can see here, it has 2 parameters:

  • file_name — the name of the file that you want to access.
  • access_mode — specifies the type of access you want for a file. Default access mode is r (reading only). Some other access modes are w (writing only), a (appending only), r+ (reading and writing both), etc.

Important : In the given syntax, r is for raw string. It enables Python interpreter to read file path correctly. For example, if we do not use r before the file path and path includes \temp then Python will interpret \t as special tab character, thereby raising an error. So, use r before the file name if it includes full path to avoid errors.

Do not forget to close the file after your done reading/writing to it. This is done with the close() method.

Reading from a file

with open('myfile.txt', 'w') as file_object :
    file_object.write('Sample line 1\nSample line 2\nSample line 3')

Python provides 3 methods to read from a file.

  • read([n]) — It allows you to read n bytes of a file. If n is not specified than it reads whole file.
  • readline([n]) — It allows you to read n bytes of a line.
  • readlines() — It allows you to read all lines of a file at once. It returns list of all lines of a file.

In the following examples, we’ve already put a file called myfile.txt in the current directory for you, so you can simply start using it! Let’s look at the most common way to read a file.

file_object = open('myfile.txt', 'r')
print( file_object.read() )
file_object.close()

If you specify the value of n in read() method, then it will read next n bytes of a file. For example, If you want to read just 5 bytes of a file, then you can write line 2 of the above code as :

file_object = open('myfile.txt', 'r')
print( file_object.read(5) )
file_object.close()

If you want to read a file line by line, then you can use readline() method. Replace read() method with readline() method in line 3. Just like read() method, you can also specify number of bytes in readline() method.

file_object = open('myfile.txt', 'r')
print( file_object.readline() )
file_object.close()

Similarly, if you want to read all lines at once. You can replace read() method with readlines() method. In this method, you will get all lines in list format.

file_object = open('myfile.txt', 'r')
print( file_object.readlines() )
file_object.close()

Alternatively, you can also use simple for loop to read a file line by line:

file_object = open('myfile.txt', 'r')
for line in file_object:
    print(line)
file_object.close()

This code will read and print whole file data line by line.

Note: You see extra new lines being printed because the print command automatically adds a newline. Since you’re printing each line individually and each of our lines in the file already end with a new line, you’ll see a double new line!

Opening a file using with statement

Python also provides an alternate way to open a file using with statement. After using with, you won’t need to remember to close a file as Python will take care of it.

Here is an example of reading a file using with statement :

with open('myfile.txt', 'r') as file_object :
  file_content = file_object.read()
  print(file_content)

It is considered good practice to use the with statement.

Writing to a file

You can write to a file using 2 methods.

  • write(str) — allows you to write string data to a file.
  • writelines([list_str]) — allows you to write a list of strings to a file.

Here is the example of writing a file in Python.

list_str = ['Line 1 \n', 'Line 2 \n', 'Line 3']
with open('myfile.txt', 'w') as file_object :
    file_object.write('Hello CommonLounge\n')
    file_object.writelines(list_str)
with open('myfile.txt', 'r') as file_object :
    file_content = file_object.read()
    print(file_content)

Note : When we use access mode w and file is not present, Python creates a new file with the given filename. If the file exists already, then the write method will overwrite the file with new data.

Saving/loading data using shelve

If you want to save some variables without using a file, then you can use the shelve module. It allows you to store data in shelf file. You can retrieve this data using the same shelve module later.

Let’s look at an example of the shelve module:

import shelve
with shelve.open('SampleData') as shelfFile:
  courses = ['Python', 'C++', 'Java']
  shelfFile['courses'] = courses    ## Storing data
  print(list(shelfFile.keys()))     ## Retrieving data
  print(list(shelfFile.values()))

You can see that shelf file is very similar to a dictionary. You can also use keys() and values() methods which will return list-like values. You should use list() function to get values in true list form.

Saving/loading data using json

JSON has became the de facto standard for sharing data — it is widely used in many applications. Python has made the use of JSON data very easy through the inbuilt module json.

This module provides direct methods for encoding and decoding JSON data.

  • dump() — reads file-like object and dumps into JSON file.
  • dumps() — reads object in string format and dumps into JSON file.
  • load() — reads JSON data and creates object .
  • loads() — reads JSON data in string format and creates object.

Let’s look at an example which describes methods for handling JSON data.

import json
## Following is the example of JSON data. 
data1 = {
            'CommonLounge' : {
                'course' : 'Python 3',
                'topic'  : 'File i/o'
            }
        }
with open("data_file.json", "w") as write_file:
    json.dump(data1, write_file)                  ## Dumping JSON data into a file
with open("data_file.json", "r") as read_file:
    data_loaded = json.load(read_file)            ## Reading JSON data
    print(data_loaded)

On line 11, we use json.dump to convert the dictionary data1 into JSON data and write it to data_file.json.

On line 14, we use json.load to read data from the file and convert it from JSON data into a dictionary.

I/O Exceptions

When you try to open a file, it is possible that the file is missing, locked or doesn’t have read/write permissions. In all these scenarios, the Python interpreter will fail to open the file. You can handle all these situations by using IOError exception.

An exception represents a run-time error that halts the normal execution at a particular line and transfers control to error handling code. For example a run-time error might be that a variable used in the program does not have a value (ValueError .. you’ve probably seen that one a few times), or a file open operation error because a file does not exist (IOError).

Without any error handling code (as we have done thus far), a run-time exception just halts the program with an error message. That’s a good default behavior, and you’ve seen it many times. Alternatively you can add a try-except structure to your code to handle exceptions, like this:

try: 
  with open('myfile.txt', 'r') as file_object :
    file_data = file_object.read() 
except IOError as e:
  print ("I/O Error : ", e.errno, e.strerror)

The try section includes the code which might throw an exception. The except section holds the code to run if there is an exception. If there is no exception, the except section is skipped (that is, that code is for error handling only, not the “normal” case for the code). You can get a pointer to the exception object itself with syntax except IOError as e: .. (e points to the exception object).

One of the most common errors while working with files is that a specified file is missing. It can happen because of invalid path or file name. This will raise an IOError . You can catch this error with IOError exception. To get more details about the exception, you can use errno and strerror methods of exception object.

Output of the above code in case of missing file is:

I/O Error : 2 No Such file or directory

Summary

In this tutorial, you’ve learned about :

  • File openingopen() method with filename and access mode parameter, use of with statement
  • File readingread([n]), readline([n]) and readlines() methods
  • File writingwrite(str), writelines(str_list) methods
  • ShelfFileshelve module, dictionary like key-value pairs
  • JSON datajson module, dump() and load() methods
  • File exceptionsIOError

© 2016-2022. All rights reserved.