Python is one of the favorite programming languages of every developer who wants to perform data analysis with files. Python comes with many inbuilt functions and methods that allow us to create, update, read and delete data between the files. In this Python tutorial, you will learn how to create files in Python? By the end of this tutorial, you will have a solid understanding on
- How to create a file in Python in the current directory
- How to create a new file if it does not exists
- How to create a file with a specified date and time
- How to create a file with specific permissions
How to Create an Empty Text file in Python?
To create a file in Python we can simply use the Python file handling open() function.
function accept two arguments
It is a string value defining the path of the file with the file's full name.
The access mode argument defines what action we want to perform on the file. Such as writing, appending, or reading data between the file. By default the
means it opens the file in reading mode. There are several
||open the file for reading data (default)|
||open the file for writing, or truncate the file first time.|
||open the file for exclusive creation, or fail if the file already exists.|
||open the file for writing, or appending to the end of new or existing file|
||open the file in binary mode binary mode|
||open the file in text mode (default)|
||open the file for updating (eg 'w+r' for writing and reading)|
Example Create a new empty file in Python
When we want to create a new file using Python, we can open the file in
access modes. The
access mode creates a new file if the file does not exist, otherwise, it uses the existing file and writes data into it.
#create a new file file = open('data.txt','w') file.close()
The above example will create a new file data.txt in the same directory where the Python script is located. We can also specify an absolute path where we want to create the file, for example.
#create a new file file = open(r'C:\Users\admin\dev\data.txt','w') file.close()
This example will create a new
file in the
directory. We also have the
access mode in Python, which is generally used when we want to create a new file in Python. The
access mode is only used to create a new file. If the file specified in the open() function with x access mode, already exist in the specified directory, you would receive the
FileExistsError: [Errno 17] File exists
#create a new file file = open('data1.txt','x') file.close()
If we only specify the file name in the open() funtion, it will consider as relative path for the file and Python generate that file in the same directory or folder where the Python script is located. After creating the empty file, we can check whether the file is generated or not. To check all the files present in the directory, we can take the help of
Check all the files present in the current directory.
import os print(os.listdir())
['data.txt', 'data1.txt', 'main.py']
We can also check if a file already exists in the directory using
import os #check if the file exist or not print("Does data.txt exist in the directory: ",os.path.isfile('data.txt'))
Does data.txt exist in the directory: True
People are also reading:
- Python Class Variables vs Instance Variables
- Python isinstance() function
- How to shuffle list in Python?
- Python range() Function Explained
- Python Take list as an input from a user
- Generate Random Float numbers in Python
- Generate Random Strings and Passwords in Python
- Nested Loops in Python
- Python random choice function to select a random item from a list and Set
- Python TypeError: ‘dict’ object is not callable solution