Example 1:
The OS module in Python includes functions for networking with the OS. All functions in the OS module throw OSError if the file names and paths are invalid or not accessible, or if other parameters have the correct type but aren’t accepted by the OS. To remove or erase a file path in Python, use the os.remove() method. A directory cannot be removed or deleted using this approach. If the supplied path is a directory, the procedure will throw an OSError. os.remove(path, *, dir_fd = None) is the syntax where a file path is represented by a path-like object called Path.
A path-like object is basically a string or a collection of bytes that describe a path. The file descriptor dir fd links to a directory. This parameter’s default value is None. dir_fd is ignored if the supplied path is absolute. The asterisk (*) in the parameter list denotes that the next arguments (in this case, ‘dir_fd’) are keyword-only parameters that can only be specified by name, not by position. One thing to note down is that the os.remove() function has no return value.
The above snapshot shows the file that is contained within the folder. We are going to remove the demo_file1 from the folder. The implementation is shown below. This Python application demonstrates the os.remove() method.
First, we imported the OS module, and then we declared the file name in the file name folder. Following that, the path to the file that we wish to remove is defined. Now that we’ve joined the path and the file with the join function, we can use the os.remove method to delete a specific file.
file_name = 'demo_file1.txt'
path = "D:/Projects/demo_files/"
file_path = os.path.join(path, file_name)
os.remove(file_path)
Here, you can see that the specified file is deleted successfully.
Example 2:
Now, we will be discussing the os.rmdir() method. os.rmdir(path, *, dir_fd = None) is the syntax to follow. The os.rmdir() function is often used to delete a blank directory. An OSError will be upraised if the stated path is not a blank directory. os.rmdir(path, *, dir fd = None) is the syntax, where a path is a path-like object that specifies a file path.
A path-like object is just a string or a collection of bytes that describe a path. The file descriptor dir_fd is optional and refers to a directory. This parameter’s default value is None. This method doesn’t return anything either.
In the above screenshot, you can view the directories. Suppose we want to remove the directory demo. The code for the removal of this directory is shown below. The os.rmdir() technique is explained in this program. We’ve already specified the directory we’d like to delete. The parent directory is then defined. Then, we’ve determined where the directory that we wish to delete is situated. Finally, os.rmdir is used to remove a directory by specifying a path.
directory_name = "demo"
full_path = "D:/Projects/"
path_name = os.path.join(full_path, directory_name)
os.rmdir(path_name)
The directory demo is successfully removed as you can see below.
Example 3:
The final example of this lesson is about the shutil.rmtree() method. Use the shutil.rmtree() function to delete a complete directory; path must link to a directory. shutil.rmtree(path, ignore_errors=False, onerror=None) is the complete syntax. It has three parameters: path, ignore_errors, and oneerror.
A file path is represented by a path-like object called Path. A path-like object is referred to as a string or a collection of bytes to show a path. If ignoring errors (another parameter) is true, errors caused by failed deletions will be entirely ignored. If ignore errors are false or omitted, such errors will be handled by calling the handler specified by onerror.
Assume the following directory structure and sub-directories. As you can see, our parent directory is a case study, and the directory inside the parent directory is case1 as you can see above. The shutil.rmtree() method is demonstrated in this Python program. The initial code shows that the OS and shutil modules have been imported. After that, we define the file location as well as the directory we want to delete. The join function is used to join the directory’s location and name, and shutil.rmtree() is used to remove the directory.
import os
loc = "D:/Projects/"
my_dir = "case study"
path = os.path.join(loc, my_dir)
shutil.rmtree(path)
The below output shows that the directory is now deleted.
Conclusion
We covered how to delete files in Python if they already exist. We deliberated over three different examples to clear the concept of deleting files in Python. Practical examples were given along with detailed explanations to assist you to grasp the idea.