Deleting Files in Python
Python supports a number of ways for removing a file or directories from the specified path.
These are by using:
- OS module’s
- pathlib module
- shutil module
Each of these ways is described below which is followed by examples.
The OS module’s remove function
A file can be removed by using the os module‘s remove function in Python. For example:
Using pathlib module:
For Python >=3.5 versions, you may also use pathlib module. For example:
file_to_rem = pathlib.Path(“tst.txt”)
file_to_rem.unlink()
Using the shutil module
By using shutil rmtree function, you may delete the entire directory (files and sub-directories).
The general way of using this function is:
For example:
See the section below for examples of each of these methods with code snippets.
The example of deleting a file by os module
For deleting a file by using the os module, you need to first import this in your python program file.
This is followed by using the remove function of os and specifying the path of the file. The example below shows how:
#Python delete file example import os os.remove("demo/test-del.txt")
If the specified file does not exist then FileNotFoundError error should be generated:
Delete file if exists – using if statement
To avoid this error, you should handle the exception by checking first if the file exists. For that, you may use the os.path.isfile. It will enable you to check the file and then delete it.
The example code is given below:
#Check if file exists? del_file = "testfile.txt" import os if os.path.isfile(del_file): os.remove(del_file) print("File removed successfully!") else: print("File does not exist!")
An example of removing an empty directory
You may also remove directories by using the os module.
To remove an empty folder, use the rmdir() function as shown below:
#Python delete directory example import os os.rmdir("demo/")
If you try removing a directory that contains files or subdirectories then OSError error should be raised:
An example of deleting a file by pathlib module
You may also use pathlib module if working on the Python 3.4+ version. The file path is set by using the path function while the unlink() function is used to remove the file from the specified path. Have a look at the code:
#Example of file deletion by pathlib import pathlib rem_file = pathlib.Path("demo/testfile.txt") rem_file.unlink()
Removing an empty directory by pathlib
Again, for removing an empty directory, you may use the pathlib’s rmdir() function. For example:
#Example of directory deletion by pathlib import pathlib rem_file = pathlib.Path("demo/") rem_file.rmdir()
Deleting files and directories by shutil module example
You may perform many high-level operations on files/directories by using shutil module.
Included in these operations are enabling to deletion of files and subdirectories by using rmtree function.
The example below shows removing a directory content that contains a file and subdirectory for the demo:
#Example of shutil import shutil shutil.rmtree('remDemo/')
As I executed the code, the remoDemo directory contained a few files (text, excel, etc.) and a subdirectory. That subdirectory had another subdirectory.
After executing the above code, the remDemo folder is removed along with all files and subdirectories.
Note, unlike the above ways of file deletion (os and pathlib modules), if you require removing the entire folder that contains multiple files and/or subdirectories; you may prefer using shutil module.
For individual file removal, you may prefer using os or pathlib modules.