How to create an empty file using Python?

Empty describes anything which contains nothing. Using a computer as an example, an empty file is one that has no data in it; it has 0 bytes and is in plain text format having no data kept in a file.

Let us discuss various types of empty files that can be created using python.

Creating an empty CSV file

The plain text file format known as a CSV file (Comma Separated Values file) is used to structure tabular data in a particular way.It can only contain actual text data that is, printable ASCII or Unicode characters—because it is a plain text file.

The structure of a CSV file is revealed by the name itself. Each distinct data value is typically separated in CSV files by a comma. The extension used for CSV file is .csv.

There are various ways to create a blank CSV file in Python. open operator is the simplest approach to create an empty csv file.It is accessible without requiring the import of any other modules. However, Python modules like pandas can also be used to generate CSV files.

Following are the multiple ways to create an empty CSV file −

Creating an empty CSV file using with open

Pass in the name of the new file and 'w' as the second parameter to make the file writable in order to create an empty csv file using with open. A new csv file is created if the file name doesn't already exist.The content will be changed if the file already exists. Pass in "a" to append the content to the file to avoid this happening.


Following is an example to create an empty CSV file with open 

with open('sample.csv', 'w') as creating_new_csv_file: pass print("Empty File Created Successfully")


On executing the above program, an empty csv file is created with the file name ‘sample’.

Empty File Created Successfully

Note − It is advisable to make sure the file doesn't already exist and, if it does, to append to it rather than overwrite it by using "a" instead of "w." If in the access mode argument 'w' parameter is given, the new material replaces any existing content in the file or leaves empty files vacant.

// Create an empty CSV file with open using 'a': with open('file.csv', 'a') as creating_new_csv_file: passCreating an empty CSV file using pandas

Some Python modules include file creation and file opening methods. It includes the pandas module.Use .to csv() method to produce a blank csv file with pandas.


The pandas module plays a key role in this illustration. Then, based on an empty list, a blank DataFrame is created. Finally, the .to csv() method is used to write the empty DataFrame. The file is generated because it doesn't already exist. pandas is installed usin the command pip install pandas in command prompt

Following is an example to create an empty CSV file using pandas

# importing the package import pandas as pd # creating new pandas DataFrame dataframe = pd.DataFrame(list()) # writing empty DataFrame to the new csv file dataframe.to_csv('file.csv') print("File Created Successfully")


On executing, the above program creates an empty csv file, displaying the following message.

Empty File Created Successfully

Creating an empty Text file

The most common sort of file is a text file, which contains text. Rich text formats and plain text can both be found in text files. A straightforward tool like Notepad can be used to create a plain text file by any computer user.

The extension used for text file is .txt


Following is the syntax used to create an empty text file:

f = open(path_of_the_file, mode)

The path to the text file you want to generate is specified by the path_of_the_text_file parameter in this syntax. A new text file is created in one of the following modes

  • Use "w" to open a file for writing. If the file doesn't already exist, the open() method is used to create it. If not, the contents of the current file will be replaced.
  • Pressing "x" will allow you to open a file for an exclusive creation. If the file is present, the open() function raises an error.


Following code writes some text into a new file called writing.txt:

with open('writing.txt', 'w') as file: pass print("Empty File Created Successfully")


On executing the above program, an empty text file is created displaying the foillowing message

Empty File Created Successfully

Note − Before generating a file in the directory for example−

'C:\Users\Lenovo\Downloads\writing.txt', you must make sure that the docs directory already exists. An exception occurs if you don't.

Following is an example to create an empty text file by providing the path:

with open('D:\writing.txt', 'w') as file: pass

This will create an empty text file is created with the name of ‘writing’ in D drive.

Creating an empty Excel file

A spreadsheet application is Excel.Excel arranges data in columns and rows, unlike word processors like Microsoft Word. Cells are the places where rows and columns intersects. Each cell is filled with information, such as text, a number, or a formula.

There are several extensions used for excel file such as xlsx for Excel Workbook, .xls for Excel 97- Excel 2003 Workbook and Microsoft Excel 5.0/95 Workbook, .xml for XML Data and XML Spreadsheet etc


Following is an example to create an empty excel file:

# importing the module from openpyxl import Workbook # create a workbook as .xlsx file def create_workbook(path): workbook = Workbook() if __name__ == "__main__": create_workbook("file.xlsx") print("File Created Successfully")


File Created Successfully

Updated on: 17-Aug-2022

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started