Python programming can be used for a variety of purposes. One such requirement is delete file with python. In this blog, we will learn how to interact with file systems using python. We will also learn how to delete/folders using various techniques. so let’s get started.
Delete file with python
Python provides various modules using which a user can delete a file or folder. The list of modules are:-
- os module
- Pathlib Module
- shutil module
In the following session, we will understand how to use these modules to delete files/folders.
Python remove file
In this session, we will understand various ways using which a user can remove a file using python
Python remove file using the os module
I have the below files and folder present in my current directory
➜ ls -l
total 8
drwxr-xr-x 2 XXXXXX staff 64 Dec 21 14:55 naive_skill
-rw-r--r-- 1 XXXXXX staff 12 Dec 21 14:56 test_python.txt
let’s try to remove the test_python.txt file.
import os
os.remove("./test_python.txt")
Python remove file using the pathlib module
Pathlib is another python library that can be used to delete a file. A user needs to create a path object first and then unlink it.
First, a user needs to import the pathlib module.
import pathlib
file = pathlib.Path("./test_python.txt")
file.unlink()
Python remove a file if exists
Many times we need to verify if the file exists before performing the delete operation. We can use the OS module to verify the file’s existence before deletion.
import os
file= "./test_python.txt"
if os.path.exists(file):
os.remove(file)
print("file is successfully removed")
else:
print("The file does not exist")
Since the file test_python.txt is present, so we get the output like the file is successfully removed
Python remove all files
Let’s create 2 files under the current directory.
➜ ls -l
total 8
drwxr-xr-x 2 XXXXXX staff 64 Dec 21 14:55 naive_skill
-rw-r--r-- 1 XXXXXX staff 12 Dec 21 14:56 test_python1.txt
-rw-r--r-- 1 XXXXXX staff 12 Dec 21 14:56 test_python2.txt
Now to delete all the files we need a for loop to traverse through all files and delete them one by one. Below is the code for the same.
import os
dir = "."
for file in os.listdir(dir):
os.remove(os.path.join(dir, file))
Python delete directory
There are several ways to delete a directory in python. In this session, we will understand all of them.
Python delete directory using the os module
The os module only deletes the directory which is empty. It can not delete a non-empty directory. For this demo, I have created two directories. One of them is blank which other has a file present in it.
tree .
.
├── naive_skill
│ └── hello.txt
└── test
Now let’s try to delete the empty directory
import os
os.rmdir("./test")
The test directory is successfully deleted. Now let’s try to delete a non-empty directory using the OS module.
we get the below error as the directory was non-empty.
Python remove a non empty directory using the shutil module
Python provides another module called the shutil using which we can perform various file operations. The Shutil module offers high-level operation on a file.
Now let’s use the shutil module to delete the non-empty directory naive_skill
import shutil
shutil.rmtree("./naive_skill")
shutil module will clean up the entire working tree irrespective of whether it is blank or not.
Python delete directory if exists
Many times a user needs to verify the directory before deleting it. This can be achieved using both the os and shutil modules. Let’s try to delete a non empty directory naive_skill
import os
import shutil
dir = "./naive_skill"
if os.path.exists(dir):
shutil.rmtree(dir)
print("directory is successfully deleted")
else:
print("directory does not exists")
The above code returns “directory is successfully deleted” because the directory was present.
Conclusion
I hope you liked this detailed tutorial on delete file with python. Please do let me know if you need additional details.