22
loading...
This website collects cookies to deliver better user experience
open()
method and pass a file path to the function.read()
, readline()
, readlines()
.close()
function.open()
function opens the file if possible and returns the corresponding file object.open()
function has a lot of parameters. Let’s take a look at the necessary params for reading the text file. It opens the file in a specified mode and returns a file object.mode
is an optional parameter. It’s a string that specifies the mode in which you want to open the file.Mode | Description |
---|---|
'r' |
Open a file for read mode (default if mode is not specified) |
'w' |
Open a file for writing. Python will create a new file if does not exist or truncates a file content if file exists |
'x' |
Open a file for exclusive creation. |
'a' |
Open a file for appending the text. Creates a new file if file does not exist. |
't' |
Open a file in text mode. (default) |
'b' |
Open a file in binary mode. |
'+' |
Open a file for updating (reading and writing) |
file = open('C:\hello.txt','r')
read()
: The read()
function returns the read bytes in the form of string. This method is useful when you have a small file, and you want to read the specified bytes or entire file and store it into a string variable. readline()
: The readline()
function returns one line from a text file and retuns in the form of string.readlines()
: The readlines()
function reads all the lines from the text file and returns each line as a string element in a list.close()
function. It is a must and best practice to perform this operation after reading the data from the file as it frees up the memory space acquired by that file. Otherwise, it may cause an unhandled exception. read()
method. The file can be opened in the read mode or in a text mode to read the data, and it can be stored in the string variable.# Program to read the entire file using read() function
file = open("python.txt", "r")
content = file.read()
print(content)
file.close()
# Program to read the entire file (absolute path) using read() function
file = open("C:/Projects/Tryouts/python.txt", "r")
content = file.read()
print(content)
file.close()
Dear User,
Welcome to Python Tutorial
Have a great learning !!!
Cheers
read()
function by specifying the bytes. The method will output only the specified bytes of characters in a file, as shown below.# Program to read the specific length
# of characters in a file using read() function
file = open("python.txt", "r")
content = file.read(20)
print(content)
file.close()
Dear User,
Welcome
readline()
function. You also use this method to retrieve specific bytes of characters in a line, similar to the read()
method.# Program to read single line in a file using readline() function
file = open("python.txt", "r")
content = file.readline()
print(content)
file.close()
Dear User,
readline()
method as shown below. This is the most effective way to read the text file line by line in Python.# Program to read all the lines in a file using readline() function
file = open("python.txt", "r")
while True:
content=file.readline()
if not content:
break
print(content)
file.close()
Dear User,
Welcome to Python Tutorial
Have a great learning !!!
Cheers
readlines()
method will read all the lines in the file and outputs in a list of strings , as shown below. Later you can use the list to traverse and extract the specified content from the list.# Program to read all the lines as a list in a file
# using readlines() function
file = open("python.txt", "r")
content=file.readlines()
print(content)
file.close()
['Dear User,\n', 'Welcome to Python Tutorial\n', 'Have a great learning !!!\n', 'Cheers']