. Advertisement .
. Advertisement .
Techniques to create a file if not exists in Python are pretty straightforward, even for people who have just set foot into these sectors. This article will illustrate the two most fundamental approaches. Check them out!
How to Create A File If Not Exists in Python?
1. Via Open()
To establish a register in Python (if it does not exist), use the open() functions. As its name suggests, the open() approach discloses a specified file and produces it as the output. It accepts the file route and mode as inputs to deliver your intended file object for the outcome sector.
Syntax of open ():
open (file, mode)
file: It is the file’s path and name.
mode: The open() algorithm adopts one of these modes:
w: The write mode. (On another note, w also helps overwriting files, which you can refer to this article for more details.)
r: The read mode.
a: is for the add mode.
w+: Should the register not exist, create it and then browse that file in the write mode.
r+: Navigate to the register for reading and writing modes.
a+: If no file existence is detected, make the files and explore them in the append (or add) mode.
These diverse modes are all available when you establish a new file. If you get a score of a+, add the content to the files or construct it if the material does not already exist.
Do you not want the file to be truncated? Then it is critical to employ the a+ mode instead of its w+ counterpart.
file = open('data.py','a+')
What happens when you execute the code above? An entry named data.py will get generated. Such files were never present before the code execution, but now it does.
In some cases, the registers already exist and incorporate particular content. Under those circumstances, a+ will not erase the material.
The w+ setting in the open() procedure also shortens your files during establishments. In the following example, we will put a few codes within the data.py file before executing the app.py in w+ mode.
file = open('data.py','w+')
Upon execution of the code above, your file will be truncated. The w command deletes existing files. docs: ‘r+’, ‘w+’, and ‘a+’ modes will facilitate updates once you open the file.
Notably, a+ will generate a particular file if it has not already existed and, critically, propels the file to its conclusion. Hence, the output will yield nothing if you immediately perform a read after opening the file in this way. Be cautious!
2. Via Path.touch()
The path.touch() operation might determine whether or not a file resides at a specified path. Initially, we set up the file’s path by employing a Path() constructor. If the files already exist, the path.touch() functions will yield no actions since the exist_ok parameter is set to True.
Now, let’s create the files using path.touch() in the following example.
from pathlib import Path file1 = Path('Article\\file.txt') file1.touch(exist_ok=True) f = open(file1)
How can I create a file if not exists in Python? This article has shed light on your burning inquiry. Both approaches work wonders for your programs, so it is up to you to decide which one is a better fit! Write to us for more clarifications (if needed).
Leave a comment