How to Delete a File in Python

Posted in /  

How to Delete a File in Python
vinaykhatri

Vinay Khatri
Last updated on December 25, 2024

    Python comes with a built-in module os that provides different methods and properties to manage operating system functionality. Using Python os module, we can create and delete files and directories between the operating system or memory. Here in this Python tutorial, we will learn how to delete a file in python. We will also learn how to delete directories in Python.

    Delete or Remove file in Python

    Often when we are working with file and directory management in Python and sometimes we want to delete individual files from our system in order to create more space or delete old files. This goal of removing files can be achieved using the Python os.remove() method.

    Remove file in Python using os.remove() method

    The Python inbuilt os module allows developers to interact with the operating system's file management system. And with the help of os.remove() method Python remove file from the memory. This means with os.remove() statement Python delete file from your system. The remove() method accepts a string value that represents the file name or location. If the python script and targeted file are present in the same directory then we can simply pass the file name. Else we need to specify the absolute path for the file location.

    syntax

    os.remove(file_location_with_file_name)

    Example

    import os
    os.remove("file.txt")

    Or

    import os
    #absolute path for the file to delete
    os.remove("C:\\Users\\ram\\Documents\\file.txt")

    If the specified file does not exist the remove method throws a FileNotFoundError . Using the os.remove() method we can delete any type of file, such as image, csv, txt, css, py , etc.

    Example: Delete an image with python

    import os
    #delete image
    os.remove("image.jpg")

    Example Delete csv file with python

    import os
    #delete csv file
    os.remove("data.csv")

    <Note>: Using the remove() method we can only delete an individual file, and we need to specify the file name along with its extension as an argument to the remove(file_name) method. We can not remove or delete a directory or folder using remove() method

    Delete Empty directory/folder using Python

    Often during programming, we want to delete empty folders or directories that are of no use, then using the os.rmdir() method we can do that. The os.rmdir() method accepts a string value as a directory name or location and deletes that directory or folder. But it can only delete the directory if the directory is empty.

    Syntax:

    os.rmdir(directory_location)

    Example

    import os
    os.rmdir("temp_directory")

    Or

    import os
    os.rmdir("C:\\Users\\ram\\Documents\\sublime\\ temp_directory “)

    If the directory is not empty the rmdir() method returns an error “OSError: [WinError 145] The directory is not empty:”

    How to delete Directory with files in Python

    If you want to delete a directory that has files in it then you can use the python shutil library rmtree() method. It is a very powerful method so be careful while using it. It works similarly to the os.rmdir() method but it is capable of removing the complete directory along with all of its files.

    Syntax

    shutil.rmtree(director_path)

    Delete folder/directory with files using Python

    import shutil
    
    shutil.rmtree("temp_directory ")

    Or

    import shutil
    shutil.rmtree ("C:\\Users\\ram\\Documents\\sublime\\temp_directory “) 

    Conclusion

    In this Python tutorial, you learned how to delete a file in Python using os module and how to delete directories in Python using shutil module. The os.remove() method can delete a file, os.rmdir() can delete empty folders, and shutil.rmtree() method can remove the directory containing one or more than one file. While using the shutil.rmtree() be double sure on the specified directory path, else you will be deleting a wrong directory and all the files along with it.

    People are also reading:

    Leave a Comment on this Post

    0 Comments