Read a Text File in Python

We show you a simple example of how to read a text file in python. The code is not very extensive.

ยป More Python Examples

with open("/home/geekole/Documents/python/files/filename.txt","r") as file:
    for line in file:
        print(line , end ="")

We will read a text file with three lines only.

When we run this code we will obtain the following result in a terminal, with:

python3 ReadTxtFile.py

Read a Text File in Python

The for loop is used to show each line in a file text, then you will see as many lines as you file has.

In the print function we set end =”” , this is because we want to prevent break lines from being duplicated.

open paramethers

In the first paramether we set the path of our text file, so you must change this.

In the second, we set how we open a file, you can change this according the following values:

rThis opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rbOpens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
wOpens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wbOpens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
w+Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
wb+Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
aOpens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
abOpens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
ab+Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

We hope this example of read a text File in Python will help you.

More: https://docs.python.org/3/tutorial/inputoutput.html

More: https://decodigo.com/python-leer-un-archivo-de-texto