To convert a file to a string in Python, use the file.read() function. The read() is a built-in Python method that returns the specified number of bytes from the file.
We can read all the content present in the file and then convert the content into a string. We can convert the file into a string and can use that string to process the operations.
with open("sample.txt", "r") as file:
data = str(file.read())
print(data)
Output
I Love Python Programming
The sample.txt file is already there in my current working directory, which has I Love Python Programming
content.
We saw how to convert a file to a string using a read function in this program. Then, we opened a file named sample.txt in a read mode in this program.
We read the file’s contents using the read() function in the next line. This read() function reads all the data present in the file. This data is then converted to a string using the str() operation and stored in a data variable. The str() function is used for converting any data into a string.
The best way to open a file is to check if the file exists or not. If the file does not exist, then an error occurs.
To check if a file exists or not in Python, use the os.path.isfile() function.
import os
path = "/Users/krunallathiya/Desktop/Code/R/sample.txt"
if os.path.isfile(path):
file = open(path, "r")
data = str(file.read())
file.close()
print(data)
Output
I Love Python Programming
In this program, we imported the os module. In the next line, we declared the path for the file. Then we check whether the file exists or not using an os.path.isfile() function.
And then, we open the file, read the content from the file and then convert that content into the string using the str() function. Then it is stored in the data variable, close in the file, and printed the content.
Converting a file to a string using readlines()
The readlines() is a built-in Python method that returns a list containing each line in the file as a list element. It returns all lines in the file as a list where each line is an element in the list object.
with open("sample.txt", "r") as f:
data = f.readlines()
content = " ".join(data)
print(content)
Output
I Love Python Programming
In this program, we opened the sample.txt file in the read mode. We used the readlines() function to get all the data lines in the next line. This data is stored in the form of a list. To convert it into a string, use the join() function. Then the list is converted to a string and is stored in a variable called the content.
Converting a file to a string using readline()
The readline() is a built-in Python method that returns one line from the file. In the readline() function, you can specify how many bytes from the line to return by using the size parameter.
Let’s convert a file to a string using the sample.txt file.
temp = ""
with open("sample.txt", "r") as f:
for line in f.readline():
temp += line
print(temp)
Output
I Love Python Programming
We created an empty string in this program and stored it in a temp variable. Then we opened the sample.txt file and used the readline() function to read line by line and concatenated each line with the temp variable. And then, we printed the temp variable. Now the temp variable consists of all the file content in a string.
Conclusion
To convert a file to a string in Python,
- Use file.read() function.
- Using readlines() function.
- Using readline() function.
That’s it for this tutorial.
More reading
How to Read First Line of File in Python
How to Save Array to File in Python
How to read binary files in Python
How to make a requirements.txt file in Python
How to Write JSON File in Python

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has experience with various programming languages and technologies, including PHP, Python, and JavaScript. He is comfortable working in front-end and back-end development.