Write Text files in Python
Let's discuss writing the text file in detail. Generally, writing text also follows the same procedure as like reading text. It varies only in some specific places.
Steps followed for writing a text file:
- Opening the text file
- Writing the text
- Closing the text file
Opening the text file:
- The open() method is used to open the text file.
- Syntax of open()
open(filepath,mode)
- As we all know, the characters inside the parenthesis of the method/function are known as Parameters. “filepath” and “mode” are Parameters defined within the method.
- Here "filepath" indicates the file's path (i.e., the location of the file) and it should be mentioned in order to access the file and open it.
- Whereas mode depends on the kind of operation, we perform. To perform a write operation, we have to use ‘w’ to overwrite the existing text. We can also use ‘a’ to append something to the end of an existing text file. If you want to create a new file, you have to mention the new file name as filepath and 'x' as mode.
- So, depending on the operations, the mode varies. As we’ve already discussed, ‘a’ is used to append. Here, append is used to insert some new text into an already existing text file at the end.
Example for Opening a text file (in write mode):
f = open(“C:\Users\Documents\Custom Office Templates”,’w’)
We have opened the file in 'w' mode, i.e., write mode, as we want to write some text in the file. This is how we use modes to open a file. We are writing the text in the "Custom Office Templates" file. The one mentioned above is the path of the “Custom Office Templates” file.
Example for Creating a new file:
f = open(“newFile.txt”,’x’)
We have opened the file in ‘x’ mode as we want to create a new text file. ".txt" is the extension of text files, and "newFile” is the name of the new file.
Writing the text:
- ‘a’ and ‘w’ modes are used to write text in a file.
- We use write() and writelines() method to write text.
- If there are multiple lines of input, write() takes the input one after the other line whereas writelines() takes the complete input of lines simultaneously.
- Syntax of write()
var.write()
- Syntax of writelines()
var.writelines()
Closing the text file:
- After writing the text, the final operation is to close the file using the close() method.
- It is mandatory to close the file as soon as the work is completed in order to prevent unnecessary access.
- It is safe if you close the files immediately after usage.
- Syntax of close() :
var.close()
- You can close the file without using the close() method also. The text file will be automatically closed using the "with" statement.
- Syntax of “with”:
with open(filepath,mode) as var1:
var2 = var1.write()
- The variable need not call the "close method" as there is an automatic close reflex, i.e., "with", to close the file.
Example programs determining opening, writing, appending, and closing operations:
Example 1:
The file already consists of the following text:
You’ve entered Custom Office Templates.
I’ve been waiting for you.
My hearty welcome to you.
Program:
f = open(“C:\Users\Documents\Custom Office Templates”,’a’)
f.write(“Hope you’re doing good”)
f.close()
# read the file after appending
f = open(“C:\Users\Documents\Custom Office Templates”,’r’)
print(f.read())
Output:
You’ve entered Custom Office Templates.
I’ve been waiting for you.
My hearty welcome to you.
Hope you’re doing good.
Explanation:
Variable “f” is assigned with the opening of the file in append mode. “f” calls the method write() to write the given text. As it is in append mode, the text is added to the text that is already present in the file. When the variable calls the read method after writing the text, all the 4 statements are printed.
Example 2:
The file already consists of the following text:
Hello !! You’re in my computer.
You have my permission to enter.
Program:
f = open(“C:\Users\MyComputer”,’w’)
f.write(“Hope you’re doing good.”)
f.close()
# read the file after appending
f = open(“C:\Users\Documents\Custom Office Templates”,’r’)
print(f.read())
Output:
Hope you’re doing good.
Explanation:
Variable “f” is assigned with the opening of the file in write mode. “f” calls the method write() to write the given text. As it is in write mode, the text will overwrite the text which is already present in the file. When the variable calls the read method after writing the text, only the statement mentioned in the write method is printed.
In this way, the output varies when the mode of operation changes. Even though the programs are similar, the output of the program varies. This is how you write text in the files.