Open a file in Python
To read the contents of a .txt file (or any file) in Python, it has to first open the file. To do this you can use the built-in open() function.
Python open() function Syntax
open(file, mode)
The open() function opens a file and returns a file object.
➡️ Ref: open() function
file: The path and name of the file. The file path and name can be "absolute" or "relative" to the current working directory.
mode: A string value defining the "mode" in which the file has to opened. There are different file access modes like,
"r" : open file to read (default)
"w" : open file for writing
"a" : open for writing but appending to the end of the file
"x" : creates a new file (you have provide a file name). it will throw an error a file with the same name already exists.
Read .txt file
As I have earlier mention in this article, you can use two different methods to read contents in a text file.
1) Using "read()" method
Syntax
file.read()
Here's the script
f_object = open("C:/Users/Arun/py/example.txt", "r") # open file in "r" (read) mode. print(f_object.read()) f_object.close() # close the text file.
The "open()" function returns a file object. The function has the file path and name, followed by the "r" mode (read mode).
To read the contents in the file, I am using the read() method in the above example. The content is printed in the terminal window.
The "read()" method returns a specified number of bytes (in the form of string) from the "file".
Note: Always use the ".close()" method to close a file.
Read specified number of characters only
You can pass an integer value (a number) as argument (parameter) to the method to get only a specified number of characters from the file. This is "optional". If "no parameter" is passed, it will read all the contents.
For example, if I want to read the first three characters only, I can do this.
print(f_object.read(3)) # read 1st three characters.
2) Using "readline()" method
Syntax
file.readline()
You can use the readline() method to read the first (or a single) line from the file.
Let us assume, I have three lines of data in the file. To get the "first" line (only), I can use the method like this.
f_object = open("C:/Users/Arun/py/example.txt", "r") print(f_object.readline()) # read the first line from the file. f_object.close()
If you want to read the first two lines, you have to write the method twice. Like this:
f_object = open("C:/Users/Arun/py/example.txt", "r") # read first two line from the file. print(f_object.readline()) print(f_object.readline()) f_object.close()
Like the "read()" method, the "readline()" also takes a parameter (or argument). You can assign a number to the method asking it to return a few characters only. For example, I want to read "10" characters from the "first line" and all the characters from the "second" line.
f_object = open("C:/Users/Arun/py/example.txt", "r") print(f_object.readline(10)) # read the first 10 characters only. print(f_object.readline()) # read the second line (all the characters). f_object.close()
Close the file
Never forget to "close" the file after opening and read the contents. To close the file use the close() method. I have used the method in all the examples above.
f_object = open("C:/Users/Arun/py/example.txt", "r")
print(f_object.readline())
f_object.close()