Create file if not exists in Python

Python create file if not exists

Creating Files in Python

File handling is a handy concept in programming. We can write and read data to files. In Python, to create or open a file, we use the open() function. It creates an object of file handling that can read or write data to files.

Ways to create file if not exists in Python

In this article, we will create a file that does not already exist in Python.

Using the open() function

As discussed earlier, the open() function creates a file handling object that opens or creates a file at the given path. There are several modes that we can use with this function.

The r mode opens files in the read mode. Similarly, the w and a modes open the given file in write and append modes, respectively. The w mode truncates the file’s content.

To create a file if it does not exist, we use the w+ and a+ mode. The a+ mode will allow us to append data to the file, and w+ will truncate the file’s contents.

We will now demonstrate how to use both these methods.

For example,

Using the pathlib.Path.touch() function

The path.touch() function can check if a file exists at a given path or not. We first set the path of the file using the Path() constructor.

We set the parameter exist_ok as True in the path.touch() function, and it will do nothing if the file exists at the given path. Now, we proceed with the open() function to create a file.

For example,

Using the os.path.exists() function

Similar to the previous method, we can use the os.path.exists() function to check whether a path exists or not. If it does exist, then the function will return True, else it returns False.

For example,

Using the try and except block

The try and except block can deal with exceptions in Python. If we open a file in the read mode and it does not exist, then an exception is raised. We can catch this using the try and except block and create the file after catching the exception.

See the code below.

Conclusion

In all the methods we discussed, the open() function only creates a file. We can set different modes within this function to create a file if it does not exist. Otherwise, we can use the touch() or exists() function to check if a path exists or not. We can also use the try and except block as shown.

That’s all about how to create file if not exists in Python.


import_contacts

You may also like:

Related Posts

  • Python absolute path
    17 November

    Get Absolute Path in Python

    Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file Conclusion We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative […]

  • Python overwrite file
    06 November

    Overwrite file in Python

    Table of ContentsFile Handling in PythonWays to overwrite file in PythonUsing the w parameter in the open() functionUsing the file.truncate() functionUsing the replace() functionUsing the pathlib moduleUsing the os.remove() function File Handling in Python We can read and write content to files in Python. We use the open() function to create files and specify the […]

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

  • Write a list to a file in Python
    12 June

    Write a list to a file in python

    Table of ContentsUsing the write() function to write a list to a file in PythonUsing the writelines() function to write a list to a file in PythonUsing the pickle.dump() function to write a list to a file in PythonUsing the json.dump() function to write a list to a file in PythonUsing the unpack operator * […]

  • Get filename from path in Python
    21 May

    Get filename from Path in Python

    Table of ContentsGet filename from Path in Python using the ntpath ModuleWindowsLinuxGet filename from Path in Python using the os.path.basename() FunctionGet filename from Path in Python using the os.path.split() FunctionGet filename from Path in Python using the pathlib.path().name MethodConclusion There are different ways to define the path of the file. In Windows, the path separator […]

  • Python read text file line by line
    13 May

    Read text file line by line in Python

    Learn about how to read text file line by line in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.