How to create files in Python?

Posted in /  

How to create files in Python?
vinaykhatri

Vinay Khatri
Last updated on November 23, 2024

    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.

    syntax

    open('file_path_with_file_name', 'access_mode')

    Arguments

    The open() function accept two arguments 1. file_path_with_file_name : It is a string value defining the path of the file with the file's full name. 2. access_mode : 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 access_mode value is 'r' means it opens the file in reading mode. There are several access_mode

    access_mode Description
    'r' open the file for reading data (default)
    'w' open the file for writing, or truncate the file first time.
    'x' open the file for exclusive creation, or fail if the file already exists.
    'a' open the file for writing, or appending to the end of new or existing file
    'b' open the file in binary mode binary mode
    't' 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 'w' or 'x' access modes. The 'w' 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 data.txt file in the C:\Users\admin\dev directory. We also have the 'x' access mode in Python, which is generally used when we want to create a new file in Python. The 'x' 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()

    Note: 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 os.listdir() function.

    Example

    Check all the files present in the current directory.

    import os
    
    print(os.listdir())

    Output

    ['data.txt', 'data1.txt', 'main.py']

    We can also check if a file already exists in the directory using os.path.isfile('file_name') function.

    import os
    
    #check if the file exist or not
    print("Does data.txt exist in the directory: ",os.path.isfile('data.txt'))

    Output

    Does data.txt exist in the directory: True

    People are also reading:

    Leave a Comment on this Post

    0 Comments