Python readfile : How to Read a File in Python ?

By ayed_amira , on 03/24/2020 , updated on 09/10/2020 - 3 minutes to read
Python readfile

Python readfile : In this article, we will see how to read a file in a python program. It will not be necessary to use a particular module to read a file. Indeed, file reading is handled natively by the python language.

So we will learn how to use python’s open() function, but first let’s review the files supported by this function.

File Types

In the Python language, a file is classified as a text type or as a binary type. This difference between the two types of files is important.

Text file

Text files are structured as a sequence of lines, where each line contains a sequence of characters. This is what you know as code or syntax.

Each line usually ends with a special character, called the end-of-line (EOL) character. The most commonly used is the comma, but a slash character can also be used, which tells the python interpreter that it should be treated as a new line.

Binary file

A binary file is a file that is not a text file (e.g. an image). Binary files cannot be processed natively by python. Indeed, you need an application that knows or understands the structure of the binary file. You will have to go through other modules or functions to be able to read these files and interpret them.

Python Read file using open() function

You can read a text file by executing the code below :

#!/usr/bin/env python

# Sets the name of the file to be read.
filename = "football.txt"

with open(filename) as f:
    text = f.readlines()

#Print the file line by line
for line in text:

It contains the list of Soccer World Cup winners by competition

2018 France
2014 Allemagne
2010 Espagne
2006 Italie
... # Others lines

The file “football.txt” must be in the same directory as your python program, if this is not the case for you, you will have to specify the absolute path.

The first line of the code stores the file name in a variable. The readlines() function reads the contents of the file line by line. We store the content in a variable called text. The for loop allows us to iterate on each line stored in the variable to display it later using the print() function.

It is important to check that the file really exists in the directory you specified. If the file does not exist, the program will raise a FileNotFoundError exception.

Here is a code to solve this problem using the isfile() function of the os python module:

#!/usr/bin/env python
import os

# Sets the name of the file to be read.
filename = "football.txt"

if not os.path.isfile(filename):
    print('File does not exist.')
    print('File exist.')
    with open(filename) as f:
        text = f.readlines()

    for line in text:

Feel free to leave a comment if you have a problem using this function, I will be happy to answer you as soon as possible! 🙂

Back to the Python section


I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)


Leave a comment

Your comment will be revised by the site if needed.