Python

Python Create File If Not Exists

Working with files is a crucial skill that is a must for a developer despite the language that he is working in. Whenever, developing such programs and applications that create a file, the developer must make sure that the program is not creating a new file every time it is executed. Rather it first checks if the file exists or not and only if it doesn’t exist, it should create a new file.

The content of this post includes the following method:

Solution 1: Using the open() Method

The open() method is commonly used to open an already existing file in Python. However, with the right flags it can be made to check if the file exists or not, and then create the file if it doesn’t exist.

Syntax of the open() Method

The syntax of the open() method includes the file designated and the opening mode to access the file with:

open(fullPathToFile, “openingMode”)

Note: There are various opening modes available, however, all of them are not the main focus of this guide. Learn opening modes of the open() method here!

The modes concerned with the problem at hand are the following:

  • “a+”: If the file is non-existent, create it and then open it using the normal “a” flag
  • “w+”: If the file is non-existent, create it and then open it using the normal “w” flag

Example: Creating a File With open() Method

Use the following lines of code to test the open() method for file creation:

file = open("demo.txt","w+");

file2 = open("HelloWorld.txt","a+")

When this code is executed, it creates the file in the same folder:

The output confirms that the file was created when it was not already present.

Side Note: In newer versions of Python, the modes “a” and “w” also create the file if it does not exist instead of causing the file to run into an error.

Let’s move on to the next method

Solution 2: Using the pathlib Module

The second best solution is to use the “pathlib” module and use its “touch()” method in the “Path” package to create the file if it doesn’t already exist using the following code

from pathlib import Path

filePath = Path("demo2.txt")

filePath.touch(exist_ok= True)

file= open(filePath,"w")

In the above code snippet:

  • First import the required package “Path”.
  • Create a file path by using the Path() method.
  • After that, run the touch() method to create a file, but include the argument “exist_ok = True” to create the file in case of its non-existence.
  • Last, open the file using the open() method and the filePath variable that we have created using the Path() method.

Upon the execution of the above code snippet, the output is as:

Text Description automatically generated

The output confirms that the file was created instead of causing the program to crash.

Conclusion

The open() built-in method can be used with the “a+” and the “w+” opening mode to create a file if it doesn’t already exist. Other than that, the user can use the touch() method from the Path package inside the pathlib module to check for an existing path and if there is no file found at the path, then it creates it. This guide has demonstrated both of these methods in detail.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.