Reading and Writing to text files in Python Program

In this tutorial, we are going to learn about file handling in Python. We can easily edit files in Python using the built-in functions.
We have two types of files that can edit in Python. Let's see what they are.
Text Files
Text files are normal files that contain the English alphabets. We call the content present in the files as text.
Binary Files
Binary files contain data in 0's and 1's. We can't understand that language.
Whenever we are working with the files in Python, we have to mention the accessing mode of the file. For example, if you want to open a file to write something in it, then it's a type of mode. Like the same way, we have different accessing modes.
Read Only - r
In this mode, we can only read the contents of the file. If the file doesn't exist, then we will get an error.
Read and Write - r+
In this mode, we can read the contents of the file, and we can also write data to the file. If the file doesn't exist, then we will get an error.
Write Only - w
In this mode, we can write content to the file. Data present in the file will be overridden. If the file doesn't exist, then it will create a new file.
Append Only - a
In this mode, we can append data to the file at the end. If the file doesn't exist, then it will create a new file.
Append and Write - a+
In this mode, we can append and write data to the file. If the file doesn't exist, then it will create a new file.
Let's see how to write data to a file.
Open a file using the open() in w mode. If you have to read and write data using a file, then open it in an r+ mode.
Open a file using the open() in w mode. If you have to read and write data using a file, then open it in an r+ mode.
Write the data to the file using write() or writelines() method
Write the data to the file using write() or writelines() method
Close the file.
Close the file.
We have the following code to achieve our goal.
# opening a file in 'w'
file = open('sample.txt', 'w')
# write() - it used to write direct text to the file
# writelines() - it used to write multiple lines or strings at a time, it takes ite
rator as an argument
# writing data using the write() method
file.write("I am a Python programmer.\nI am happy.")
# closing the file
file.close()
Go the directory of the program, and you will find a file named sample.txt. See the content in it.
We have seen a method to write data to a file. Let's examine how to read the data which we have written to the file.
Open a file using the open() in r mode. If you have to read and write data using a file, then open it in an r+ mode.
Open a file using the open() in r mode. If you have to read and write data using a file, then open it in an r+ mode.
Read data from the file using read() or readline() or readlines() methods. Store the data in a variable.
Read data from the file using read() or readline() or readlines() methods. Store the data in a variable.
Display the data.
Display the data.
Close the file.
Close the file.
We have the following code to achieve our goal.
# opening a file in 'r'
file = open('sample.txt', 'r')
# read() - it used to all content from a file
# readline() - it used to read number of lines we want, it takes one argument which
is number of lines
# readlines() - it used to read all the lines from a file, it returns a list
# reading data from the file using read() method
data = file.read()
# printing the data
print(data)
# closing the file
file.close()
If you run the above program, you will get the following results.
I am a Python programmer.
I am happy.
I hope you understand the tutorial. If you have any doubts, mention them in the comment section.