How to create a new file in Python [2 different ways]

Do you want to create a new file in Python? In this Python Tutorial, I will explain “How to create a new file in Python” using the in-built functionalities of Python.

To create a new file in Python, there is one built-in function of python called open(). By giving a parameter as “w” or “x” in file mode you can create a new file in Python.

Different ways to Create a New file in Python

  • By using “w” as a parameter
  • By using “x” as a parameter

Create a New File if the file does not exist in Python

Let’s understand how we can use the open() method and “w” as a parameter in Python. The open() function will return a file object which we can use to create a new file in Python, read a file, edit a file content, etc.

And, file mode as “w” in the open() method will open the file in the write mode. So, when we write open() with “w” then, we are creating a new file that opens up in a write mode.

Let’s take one example to create a new file in Python using “w” as a parameter.

Code :

file_path = "index.txt"

with open(file_path,"w") as main_file:
    content = "Welcome to Python Guides...\nLearn Python With Practical Examples"
    main_file.write(content)
    print("File has been saved successfully to your directory")

Output: Here is the screenshot that shows the result after I executed the code in the visual studio:

how to create a new file in python

Let’s take another example to create a new file in a different directory.

Code :

file_path = "C:\\Users\\Asus\\OneDrive\\Documents\\demo.txt"

with open(file_path,"w") as main_file:
    content = "Welcome to Python Guides...\nLearn Python With Practical Examples"
    main_file.write(content)
    print("File has been saved to your directory")

Note: Observe the changes in file_path. If you give a single backslash, you will face some errors in Python.

So, we can use the double backslash instead of a single or single forward slash, to make it read as a raw string in Python.

Also, you can give “r” at the beginning of the path to make it a raw string in Python, and work with a single backslash, like this:
file_path = r”C:\Users\Asus\OneDrive\Documents\demo.txt”

Create a new file if the file does not exist

Let’s check how the “x” parameter will work to create a new file in Python. When we give mode “x” as a parameter to the open() method, it will also check whether the name of the file already exists or not.

If the file does not exist, then it will create a new file otherwise it will raise FileExistsError error in Python if the file already exists.

The “x” parameter in the open() method will open the file in the exclusive creation mode.

Here is an example:

file_path = "index.txt"

with open(file_path,"x") as main_file:
    content = "Welcome to Python Guides...\nLearn Python With Practical Examples"
    main_file.write(content)
    print("File has been saved to your directory")

Output:

Here is the screenshot of the execution of the code in the visual code studio:

create new file in python if it doesn't exist

Conclusion

In this tutorial, I have explained “How to create a new file in Python” in different ways using different parameters in the open() method like “w” and “x“. I have taken some examples so you can understand the difference between both parameters.

I hope you understand these methods and can easily create a new file in Python without any errors.

You may also like to read: