How to Read Text File in Python?
Read Text File in Python
To read text file in Python, follow these steps.
- Call open() builtin function with filepath and mode passed as arguments. open() function returns a file object.
- Call read() method on the file object. read() returns a string.
- The returned string is the complete text from the text file.
Examples
1. Read a text file "sample.txt"
In the following Python program, open()
function opens sample.txt
in read mode. read()
method reads all the contents of the text file, and print()
displays the text on the console.
Python Program
fileObject = open("sample.txt", "r")
data = fileObject.read()
print(data)
Explanation
open()
function with the file name and mode"r"
opens the file in read mode.read()
method retrieves all the content of the file as a string.print()
function displays the retrieved data on the console.
Output
Welcome to pythonexamples.org
You can provide the complete or absolute path to the open()
function or use a relative path if the base path is present in the PATH environment variable.
2. Read only a few characters from the text file
If you need to read a specific number of characters (e.g., N characters) from the start of the file, pass N as an argument to the read()
function.
In the following Python program, read(20)
retrieves the first 20 characters from the file.
Python Program
f = open("sample.txt", "r")
data = f.read(20)
print(data)
Explanation
open()
function with the file name and mode"r"
opens the file in read mode.read(20)
retrieves the first 20 characters from the file.print()
function displays the retrieved characters.
Output
Welcome to pythonexa
The read(20)
function returns the first 20 characters from the text file.
3. Read the file in text mode
You can specify text or binary mode based on the nature of the content. In this example, open()
function explicitly opens the file in text mode using "t"
along with the read mode "r"
.
Python Program
f = open("sample.txt", "rt")
data = f.read()
print(data)
Explanation
open()
function with mode"rt"
explicitly opens the file in text read mode.read()
method retrieves all the content of the file as a string.print()
function displays the retrieved data.
Output
Welcome to pythonexamples.org
4. Read the text file line by line
readline()
method retrieves one line at a time from the file and moves the pointer to the next line. You can call readline()
in a loop to read all lines sequentially.
Remember, readline()
includes the newline character at the end of each line, except for the last line. Use strip()
to remove the newline character if required.
In this example, readline()
reads the text file line by line.
Python Program
# Get file object
f = open("sample.txt", "r")
while(True):
# Read the next line
line = f.readline()
# Break the loop if there are no more lines
if not line:
break
# Print the line without newline character
print(line.strip())
# Close the file
f.close
Explanation
open()
function with mode"r"
opens the file in read mode.readline()
retrieves the next line from the file.while
loop iterates untilreadline()
returns an empty string, indicating the end of the file.strip()
method removes newline characters and leading/trailing spaces from each line.print()
function displays each line without unnecessary characters.close()
function closes the file to release resources.
Output
Hi User!
Welcome to Python Examples.
Continue Exploring.
Summary
In this tutorial from Python Examples, you learned how to read a text file in Python using detailed example programs.