Python - Create New File


Python - Create New File

To create a new file in Python, use the open() method with the filename as the first argument, and "x" as the second argument. This opens the file in exclusive creation mode, ensuring the file is only created if it does not already exist.

The syntax to create a file with a specific filename is as follows:

myfile = open("complete_filepath", "x")

Here, the second argument, "x", specifies that the file should be opened in exclusive creation mode, meaning the file will only be created if it does not exist already.


Examples

1. Create a New File using open()

In this example, we will create a new file named sample.txt in the current working directory.

Python Program

# Open file in exclusive creation mode
f = open("sample.txt", "x")

# Close file after creation
f.close()

Explanation:

  1. The open() function is used to create the file sample.txt.
  2. The "x" mode ensures that a new file is created only if sample.txt does not already exist in the directory.
  3. The file is closed after creation using f.close().

A new file will be created in the current working directory. You can also provide a complete path if you want the file to be created at a specific location.


2. Create a New File with the Same Name as an Existing File

In this example, we will attempt to create a new file with the same name as one that already exists, sample.txt.

Python Program

f = open("sample.txt", "x")
f.close()

Explanation:

  1. The code attempts to create sample.txt using the "x" mode.
  2. Since sample.txt already exists, the program will raise a FileExistsError.
  3. The file is closed using f.close() (though the file creation will not succeed).

Output

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    f = open("sample.txt", "x")
FileExistsError: [Errno 17] File exists: 'sample.txt'

3. Create a File with a Specific Path

In this example, we will create a new file at a specified path using the open() method.

Python Program

f = open("/path/to/directory/sample.txt", "x")
f.close()

Explanation:

  1. The file sample.txt will be created at the specified directory /path/to/directory/.
  2. The "x" mode ensures that the file is only created if it does not already exist at that location.
  3. After creation, the file is closed using f.close().

4. Attempt to Create a File in a Non-Existent Directory

In this example, we will attempt to create a new file in a directory that does not exist.

Python Program

f = open("/nonexistent_folder/sample.txt", "x")
f.close()

Explanation:

  1. The code attempts to create a new file sample.txt in a folder that does not exist.
  2. Since the directory does not exist, this will raise a FileNotFoundError.
  3. File creation will fail unless the directory is created first.

Output

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    f = open("/nonexistent_folder/sample.txt", "x")
FileNotFoundError: [Errno 2] No such file or directory: '/nonexistent_folder/sample.txt'

Summary

In this tutorial, we learned how to create a new file in Python using the open() function with the "x" mode. We covered scenarios such as successfully creating a new file, handling errors when the file already exists, and managing file creation at a specific path. This knowledge is essential for performing file operations in Python.


Python Libraries