How To Delete File In Python - Scaler Topics
Maybe your like
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 | os.remove() |
|---|---|---|
| Pathlib | path_object.unlink() | |
| Deleting Empty Directories? | Os | rmdir() |
| Pathlib | ||
| Deleting non-empty Directories? | Shutil | rmtree() |
Tag » How To Delete File In Python
-
Python Delete File - W3Schools
-
How Do I Delete A File Or Folder In Python? - Stack Overflow
-
How To Delete A File In Python
-
Python Delete File: A Step-By-Step Guide | Career Karma
-
How To Create And Delete A File In Python? - ProjectPro
-
Delete A Directory Or File Using Python - GeeksforGeeks
-
Delete (Remove) Files And Directories In Python - PYnative
-
Python Remove File - How To Delete A File? | Flexiple Tutorials
-
Python Delete A File 🗑️ - YouTube
-
How To Delete A File Or Folder Using Python - Data To Fish
-
Python Delete File [Ultimate Guide] - Finxter
-
Python File Operations - Read And Write To Files With Python
-
Python Remove Or Delete File
-
How To Delete A File In Python – With Example - CodeBerry