# How to read a text file in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

We can read a text file in Selenium with python by first creating a txt file and having a content on it.

First of all, we need to open the file and mention the path of the location of the text file as an argument. There are multiple reading methods to perform these operations.

• readline() – It reads character line by line at a time. If we need to read the first two lines, the readline() method is to be used twice.

• readlines() – it reads line by line and stores them in a list.

## Example

#open the file for read operation
f = open('pythontext.txt')
#reads the entire file content and prints in console
#close the file
f.close()

#open the file for read operation
f = open('pythontext.txt')
#reads 4 characters as passed as parameter and prints in console
#close the file
f.close()

#open the file for read operation
f = open('pythontext.txt')
while l!= "":
print(l)
#close the file
f.close()

#open the file for read operation
f.close()