Python comes with a built-in module
that provides different methods and properties to manage operating system functionality. Using Python
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
Remove file in Python using
The Python inbuilt
module allows developers to interact with the operating system's file management system. And with the help of
method Python remove file from the memory. This means with
file from your system. The
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.
import os os.remove("file.txt")
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
method we can delete any type of file, such as image,
csv, txt, css, py
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")
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
method. We can not remove or delete a directory or folder using
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
method we can do that. The
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.
import os os.rmdir("temp_directory")
import os os.rmdir("C:\\Users\\ram\\Documents\\sublime\\ temp_directory “)
If the directory is not empty the
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
method. It is a very powerful method so be careful while using it. It works similarly to the
method but it is capable of removing the complete directory along with all of its files.
Delete folder/directory with files using Python
import shutil shutil.rmtree("temp_directory ")
import shutil shutil.rmtree ("C:\\Users\\ram\\Documents\\sublime\\temp_directory “)
In this Python tutorial, you learned how to delete a file in Python using
module and how to delete directories in Python using
method can delete a file,
can delete empty folders, and
method can remove the directory containing one or more than one file. While using the
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:
- How to use split in Python?
- Print without newline in Python
- How to update all Python Packages?
- Invalid Syntax Python
- How to loop with indexes in Python?
- Python Absolute Value
- How to Import an Excel File into Python using Pandas?
- Math in Python 3 with Operators
- How to Install Python 3 on Ubuntu 18.04 or 20.04
- Use sorted() and sort() in Python