Python Write/Create Files

As discussed in the python file opening, the built-in open() function is crucial for working with the files. To perform any file-related operations such as creating the file, writing the file, reading the file, etc., you need to use the open() function in python.

Create a New File

In python, you can create a file by using the open() method with x mode.

 

Following is the example of creating a new file using x mode in python.

 

file = open("testfile.txt", "x")

The above example will create an empty file, but it will throw an error if it already exists in the directory.

 

The x mode will create a new file when the defined file does not exist in the directory. Otherwise, it will throw an error. To solve this problem, you need to use either w or a mode with an open() function.

 

file = open("testfile.txt", "w")

#or

file = open("testfile.txt", "a")

The modes, either w or will create a file only when the specified file does not exist.

Write to a File

To write a text to the existing file or create a new file that does not exist, you need to use w mode with open() function in python. The w mode will always treat the file as a new file and overwrite the existing content.

 

After opening the file in writable (w) mode, you need to use the write() method to store the content in the file. Following is the example of writing text to the file in python.

 

file = open("D:\\testfile.txt", "w")
file.write("Welcome to Tutlane")
file.close()

#Open and read the file content
file = open("D:\\testfile.txt", "r")
print(file.read())

If you observe the above example, we are opening the file using open() method in writable (w) mode. After that, we are writing the text to the file using write() method.

 

The above example will overwrite the text of the testfile.txt file in the D drive. If the file (testfile.txt) does not exist in the D drive, it will create and add text to that file.

 

When you execute the above program, you will get the result as shown below.

 

Welcome to Tutlane

Like the write() method, we have another method called writelines() in python to save the list object content in the file. To keep the list object values in separate lines, you need to add a new line character (\n) to the list values.

 

Following is the example of writing text to the file using writelines() method in python.

 

lst = ["Hi\n", "Welcome to Tutlane\n"]
file = open("D:\\testfile.txt", "w")
file.writelines(lst)
file.close()

#Open and read the file content
file = open("D:\\testfile.txt", "r")
print(file.read())

If you observe the above example, we are writing text to the file using the writelines() method and added the new line character (\n) to the list values to save the list values in separate lines.

 

The above example will return the result as shown below.

 

Hi
Welcome to Tutlane

Append Text to a File

As discussed, the w mode will always overwrite the existing file content. If you want to append text to the file without losing the current content, you need to use the open() method with a or a+ mode in python. The a or a+ modes will append text to the end of the file.

 

Following is the example of appending text to the file using the open() function with a mode in python.

 

file = open("D:\\testfile.txt", "a")
file.write("Welcome to Tutlane")
file.close()

#Open and read the file content
file = open("D:\\testfile.txt", "r")
print(file.read())

The above example will return the output as following because the w mode will overwrite the existing file content.

 

Welcome to Tutlane

This is how you can create a file and write/append text to python files based on your requirements.