Python | Create a New File Programmatically

In Python or any other programming language, various types of operations can be performed on the file. These operations may include creating new files, writing files, reading files, reading line by line, etc. Python provides various inbuilt functions to perform all of these operations, such as “open(), “close”, “write()”, etc.
Throughout this write-up, we will provide a comprehensive explanation of creating a new file programmatically in Python. The following terms will be discussed in this Python write-up:

So, let’s begin our Python guide!

How to Create a New File Programmatically?

The “open()” function creates a new file in Python programs. The syntax of the “open()” function is shown below:

f = open(filepath, mode)

In the above syntax:

  • In place of the ” filepath” parameter, you can specify the complete path or just the name of the file.
  • The parameter “mode” indicates the different modes of operation for the “open()” function, such as opening, creating, appending, etc.
  • To create an exclusive new file using the open() function, the “x” character will be used in place of the “mode” parameter.

Method 1: Using the open() Function to Create a New File

In the example shown below, the “open()” function is used to create a new file programmatically in Python script.

Code:

#open and create file
filename = open("F:\itslinuxfoss\Documents\sample.txt", "x")

#closing the file
filename.close

In the above code, the “open()” function accepts two parameters. The first one indicates the complete path of the file, while the second parameter, “x”, represents the mode. The file is closed using the function named “filename. close”.

Note: If the complete path is not specified, the file will be created in Python’s current working directory by default.

Output:

In the above output snippet, the new file named “sample” is created.

Method 2: Create a File With an Existing File Name

Let’s utilize the open() function to create a new file with the same name of the file, i.e., “sample.txt” located in our system:

Code:

#open and create file
filename = open("F:\itslinuxfoss\Documents\sample.txt", "x")

#closing the file
filename.close

In the above code, the same file name and path are used to create a file in Python.

Output:

From the above snippet, we can see that the error named “FileExistsError” occurred in the program. To rectify an existing file name error in Python, the try-except block is utilized in the program. Let’s understand it via the following code:

Code:

try:
    filename = open("F:\itslinuxfoss\Documents\sample.txt", "x")

except FileExistsError:
    print("Creating a file with new name")
    filename = open("F:\itslinuxfoss\Documents\sample1.txt", "x")

#closing the file
filename.close

In the above code, the “open()” function is initialized inside the “try” block of the python program. When the “open()” function executes, the “FileExistsError ” will occur, which will be handled in the “except” block.

Output:

As shown in the above snippet, the file with the new name has been created at the specified location.

How to Remove a File in Python?

The “remove()” function of the “os” module is used to remove the file in the below code example.

Code:

import os
os.remove("F:\itslinuxfoss\Documents\sample1.txt")

In the above code, the “os.remove()” function takes the path inside the parentheses and deletes the file.

Output:

As shown in the above snippet, the file with the name “sample1” has been deleted from the specified location.

This concludes our guide!

Conclusion

To create a new file programmatically in Python, the “open()” function is used in the program. The “open()” function creates the file in Python’s current working directory or the user-specified path. To create an existing file, the “try-except” block technique is utilized in the program. To remove a file in python, the function “os.remove()” is used. This article presented a complete guide on creating a new file programmatically in Python.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×