Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Create an Empty File in Python

  • 30 Jul, 2025
  • Com 0
Creating an empty file in Python

To create an empty file in Python, use the “with open()” statement with write mode (‘w’) and add the pass statement, which ensures no content is written.

Python create an empty file

file_name = "empty_file.txt"

with open(file_name, 'w') as f:
    pass  # Ensure no content is written

print(f"File '{file_name}' created successfully (or overwritten).")

# Output: File 'empty_file.txt' created successfully (or overwritten).

Empty text file created using Python

This approach is ideal for creating a new file or resetting an existing one to an empty state.

However, if an existing file with the content already exists, it will override the new file, potentially causing data loss, which is the main con of this approach.

What if the file already exists?

If the file already exists and you don’t want to override, use the with open() statement with ‘x’ (exclusive creation ) mode because it throws FileExistsError and prevents accidental overrides.

file_name = "empty_file.txt"

try:
    with open(file_name, 'x') as file:
        pass
except FileExistsError:
    print("File already exists!")

# Output: File already exists!

We used the try/except mechanism to handle the FileExistsError, but this way, it won’t create an overriding file.

Creating multiple empty files

To create multiple empty files, we can use the modern approach (Python 3.4+) using the pathlib module. It provides the Path.touch() method, which helps us create a file. Using a for loop, we can create multiple files.

The Path.touch() method creates an empty file or updates the timestamp of an existing one without modifying its content.

from pathlib import Path

for i in range(3):
   Path(f'output/empty_file_{i}.txt').touch()

print("Empty files created in 'output' directory.")

# Output: Empty files created in 'output' directory.

Here is the folder before creating multiple files:

Empty directory

After running the above program, we have 3 empty files like this:

Creating multiple empty files

Use pathlib.Path.touch() for simplicity and cross-platform compatibility.

Creating an empty file in a non-existent directory

If you attempt to create a file in a directory that doesn’t exist, it raises FileNotFoundError.

To handle that error, use os.makedirs() method, which will create a non-existent directory.

import os
from pathlib import Path

# Create directory if it doesn't exist
os.makedirs('new_folder', exist_ok=True)

Path('new_folder/empty_file.txt').touch()

Creating an empty file in a non-existent directory

Why do we need to create a blank file?

  1. We can reserve a filename for future use. For example, log files or data config files.
  2. It creates files for testing file operations.
  3. There are some applications that require an empty file to exist before processing.
Post Views: 502
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Numpy.arange() Method
How to Read and Write a List to a File in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend