HomeLinuxPython Create File If Not Exists

Python Create File If Not Exists


Working with recordsdata is an important ability that may be a should for a developer regardless of the language that he’s working in. Each time, growing such applications and purposes that create a file, the developer should be sure that this system isn’t creating a brand new file each time it’s executed. Quite it first checks if the file exists or not and provided that it doesn’t exist, it ought to create a brand new file.

The content material of this submit contains the next methodology:

Answer 1: Utilizing the open() Methodology

The open() methodology is usually used to open an already present file in Python. Nonetheless, with the appropriate flags it may be made to verify if the file exists or not, after which create the file if it doesn’t exist.

Syntax of the open() Methodology

The syntax of the open() methodology contains the file designated and the opening mode to entry the file with:

open(fullPathToFile, “openingMode”)

Be aware: There are numerous opening modes accessible, nevertheless, all of them should not the principle focus of this information. Study opening modes of the open() methodology right here!

The modes involved with the issue at hand are the next:

  • “a+”: If the file is non-existent, create it after which open it utilizing the conventional “a” flag
  • “w+”: If the file is non-existent, create it after which open it utilizing the conventional “w” flag

Instance: Making a File With open() Methodology

Use the next strains of code to check the open() methodology for file creation:

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

file2 = open(“HelloWorld.txt”,“a+”)

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

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

Aspect Be aware: In newer variations of Python, the modes “a” and “w” additionally create the file if it doesn’t exist as an alternative of inflicting the file to run into an error.

Let’s transfer on to the subsequent methodology

Answer 2: Utilizing the pathlib Module

The second finest answer is to make use of the “pathlib” module and use its “contact()” methodology within the “Path” bundle to create the file if it doesn’t exist already utilizing the next code

from pathlib import Path

filePath = Path(“demo2.txt”)

filePath.contact(exist_ok= True)

file= open(filePath,“w”)

Within the above code snippet:

  • First import the required bundle “Path”.
  • Create a file path through the use of the Path() methodology.
  • After that, run the contact() methodology to create a file, however embody the argument “exist_ok = True” to create the file in case of its non-existence.
  • Final, open the file utilizing the open() methodology and the filePath variable that we’ve created utilizing the Path() methodology.

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

Text Description automatically generated

The output confirms that the file was created as an alternative of inflicting this system to crash.

Conclusion

The open() built-in methodology can be utilized with the “a+” and the “w+” opening mode to create a file if it doesn’t exist already. Apart from that, the consumer can use the contact() methodology from the Path bundle contained in the pathlib module to verify for an present path and if there isn’t a file discovered on the path, then it creates it. This information has demonstrated each of those strategies intimately.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments