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.

Was this post helpful?

Related Posts

  • 19 May

    Remove Extension From Filename in Python

    Table of ContentsHow to Remove Extension From Filename in Python?Remove Extension From Filename in Python Using the os ModuleRemove Extension From Filename in Python using the pathlib moduleRemove Multiple File Extensions Using pathlib Module in PythonConclusionWas this post helpful? While programming in python, we often have to deal with file names. In this article, we […]

  • 19 May

    Get Directory Name From File Path in Python

    Table of ContentsHow to Get directory name from file path in python?Get Directory Name From File Path Using the os Module in PythonGet Directory Name From File Path Using the pathlib ModuleRemove FilnName from File PathRemove Filename From the File Path Using the OS Module in PythonRemove Filename From File Path Using the Pathlib Module […]

  • 19 May

    Print Current Directory in Python

    Table of ContentsFiles and Directories in PythonWays to print the Current Directory in PythonUsing the os.getcwd() function to print the current directory in PythonUsing the os.path module to print the current directory in PythonUsing the os.path.abspath() function to print the current directory in PythonUsing the pathlib.Path.cwd() function to print the current directory in PythonUsing the […]

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusionWas this post helpful? In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • 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 ConclusionWas this post helpful? We often use files to store the outputs of our programs in permanent storage. Generally, we […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.