Delete a File in Python
Python programmers use files for a variety of purposes. One of the most crucial functions you need to know when working with files is how to delete a file.
Take, for example, a program that analyses the performance of the S&P 500 index and saves the results in a file. You may want to delete any existing analysis files to make room for the new file.
We can use the os.remove() method in Python to remove files and the os.rmdir() method to delete an empty folder. We can use the shutil.rmtree() method to delete a folder and all its files.
Methods to Delete Files in Python
Removing the files or a single file from a particular directory when it is no longer required is the basic concept of deleting a file. Now to remove a file, you have three methods. Using one of the modules:
- os Module
- shutil Module
- pathlib Module
1. Using os Module
The os module allows you to use the operating system-dependent functionalities.
To use the os module to delete a file, we import it, then use the remove() function provided by the module to delete the file. It takes the file path as a parameter.
You can not just delete a file but also a directory using the os module.
To delete a file:
In this piece of code, we’re first importing the os module and then storing the complete file path of the file we wish to delete in a variable called file_path. We then check if the file exists at that path, then we remove the file, and if it doesn’t exist, then we do nothing.
On the other hand, if you wish to delete or clear a directory, you can do it with the help of the rmdir() function of the os module. Note that the directory must be empty for this to work.
If the directory to be deleted is in the same directory as the Python program, then you need not provide a full path. A relative path will work. Otherwise, a path can be written as the parameter of the rmdir function.
2. Using shutil Module
The shutil module is a high-level file operation module. You can perform functions like copying and removal of files and collections of files.
This module can also be used as the os module to delete files or a directory. But here, the directory is not necessary to be empty. If you delete a directory using shutil, you can also delete all the contents inside of it (files and subdirectories).
The function of the shutil module is rmtree().
Here is the place of path; We can provide the path to our directory that you wish to remove.
You cannot delete a single file with the shutil.rmtree() function.
3. Using pathlib Module
If you’re working with a Python version 3.4+, then the pathlib module is beneficial for deleting/removing files.
The pathlib module has many similarities with the os module, two of them being the remove and rmdir methods.
When working with shutil module, you must first create a Path object. When an instance of the Path class is created, a WindowsPath or PosixPath will be returned according to the machine you’re working on. A WindowsPath object will be returned for Windows OS, and for non-windows OS like Linux, PosixPath will be returned.
Then, the next step is to use the unlink() function. The unlink() function removes the file or the symbolic link. If you wish to delete a directory, you must use the rmdir() function instead.
In the same way, if you want to delete a directory:
But again, the rmdir() function only allows you to delete empty directories.
We can summarise it all with the following table:
|Deleting a single file?||Os||
|Deleting Empty Directories?||Os||
|Deleting non-empty Directories?||Shutil||
After learning about Deleting Files in Python, here are some key points that will help you:
- In Python, we mainly have three methods for deleting files.
- The os module in Python allows us to use the operating system-dependent functionalities.
- If you delete a directory using shutil, you can also delete all the contents inside of it (files and subdirectories).
- The unlink() function removes the file or the symbolic link.