While working on projects, many times there are so many demo files or directories which you have to remove because they are not required anymore while cleaning your code base. So, every programmer needs to know how to delete a file in python. We will explain different ways to delete files in directories.
In python following modules enable you to perform this type of task:
- Import os: Os module allows us to use operating system-dependent functionalities.
- From pathlib import Path: It is similar to the OS module only difference is in this we create a Path object for a particular file to pass it in function.
- Import shutil: Shutil is a high-level module for file operations. On individual files and sets of files, you can carry out operations like copying and removing.
The main difference between the OS module and the Shutil module is that in the OS module, it is necessary that the directory you want to delete should be empty, but this is not the case in shutil module, it can delete the whole directory with its contents.
It is a best practice to first check for the existence of a file or folder before deleting it. After that perform the action to avoid errors. You can check for the file's existence using the following commands:
- os.path.isfile("/path/to/file")
- Use exception handling (Try - Except block)
- os.path.exists
Note: The file might be deleted or altered between the two lines, that's why exception handling is advised rather than checking.
How to Delete a File in Python?
Let's now look at 5 different methods to remove a file from a directory using python:
Method 1) os.remove(file_path)
The os.remove(path) function is used to delete a file from the file system whose path is passed. The path argument is a string that specifies the path of the file that you want to delete. If the file exists, it will permanently remove it. If it doesn't, it throws a FileNotFoundError exception.
Hence, to avoid the error we always prefer to check for file existence.
Example:
import os from pathlib import Path import touch myfile = "favtutor.txt" Path(myfile).touch() #creating a new empty file print("Files in a directory -->",os.listdir()) print("We can see a file created succesfully") print("-----------------------------------------------------") # If file exists, delete it. if os.path.isfile(myfile): os.remove(myfile) print("Favtutor file deleted using remove() function") print("Current files in directory -->",os.listdir()) print("-----------------------------------------------------") else: # If it fails, inform the user. print("Error: %s file not found" % myfile)
Output:
Files in a directory --> ['demo.txt', 'favtutor.txt', 'jupyter.ipynb', 'SPAM text message 20170820 - Data.csv'] We can see a file created succesfully ----------------------------------------------------- Favtutor file deleted using remove() function Current files in directory --> ['demo.txt', 'jupyter.ipynb', 'SPAM text message 20170820 - Data.csv'] -----------------------------------------------------
Note that If any files are once deleted using python, They will be deleted permanently. So, be cautious!
Method 2) os.rmdir(directory_path)
The os.rmdir(directory_path) function is used to remove an empty directory from the file system. The "rmdir" means to remove the directory. If the directory specified by directory_path exists and is empty, this function will delete it. If it is not empty, the function will raise an OSError exception with the message "Directory not empty".
Keep in mind that if the directory is the same as your python program you can pass the relative path to it otherwise you have to pass the full path of the directory you want to delete inside the function.
Example:
import pathlib path="Text_files" # specifiing new folder name if not os.path.exists(path): # checking for folder existance # creating a new folder os.makedirs(path) # fetching the working directory path rootdir=os.getcwd() #loop to iterate working directory files and subdirectory for root, dirs, files in os.walk(top=rootdir, topdown=False): for name in dirs: dir_path=os.path.join(root, name) print("Subdirectory",dir_path) print("We can see a folder created succesfully with name Text_files") print("-----------------------------------------------------") #creating a new empty file into a subdirectory myfile = "favtutor.txt" new=os.path.join(dir_path,myfile) Path(new).touch() print("Files in a directory -->",os.listdir()) # Try to delete the empty folder # checking for folder availaibility using try and except block try: os.rmdir(dir_path) for root, dirs, files in os.walk(top=rootdir, topdown=False): if dirs==[]: print("No subdiretectory") else: print(dirs) print("folder deleted succesfully") print("-----------------------------------------------------") except OSError as e: print("Error: %s - %s." % (e.filename, e.strerror)) print("-----------------------------------------------------")
Output:
Subdirectory f:\projects\Favtutor\Text_files We can see a folder created succesfully with name Text_files ----------------------------------------------------- Files in a directory --> ['demo.txt', 'favtutor.txt', 'jupyter.ipynb', 'SPAM text message 20170820 - Data.csv', 'Text_files'] Error: f:\projects\Favtutor\Text_files - The directory is not empty. -----------------------------------------------------
As we have created a new file named “Favtutor” inside our newly created subdirectory, rmdir() function fails to delete the directory, giving us the error stating that the folder is not empty. Now let's try deleting the folder after first deleting the file so that our directory is empty.
try: os.remove(new) print(new,"Favtutor file deleted using remove() function") os.rmdir() for root, dirs, files in os.walk(top=rootdir, topdown=False): if dirs==[]: print("No subdiretectory") else: print(dirs) print("folder deleted succesfully") print("-----------------------------------------------------") except OSError as e: print("Error: %s - %s." % (e.filename, e.strerror)) print("-----------------------------------------------------")
Output:
f:\projects\Favtutor\Text_files\favtutor.txt Favtutor file deleted using remove() function No subdiretectory folder deleted succesfully -----------------------------------------------------
Now, as the directory was empty, rmdir() method deleted the directory successfully.
Method 3) shutil.rmtree(path[, ignore_errors[, onerror]])
The shutil.rmtree(path) function is used to remove a directory and all of its contents, including all subdirectories and files. The ignore_errors is an optional Boolean argument to whether or not to ignore any errors occurred during the deletion process. So, a major drawback is that you cannot delete a single file using it.
As in the above example we faced the issue of the directory not being empty, we can use shutil() method in place of rmdir() to delete the directory completely without any error. The code will have just a few slight changes as below:
Example:
import pathlib import shutil path="Text_files" # specifiing new folder name if not os.path.exists(path): # checking for folder existance # creating a new folder os.makedirs(path) # fetching the working directory path rootdir=os.getcwd() #loop to iterate working directory files and subdirectory for root, dirs, files in os.walk(top=rootdir, topdown=False): for name in dirs: dir_path=os.path.join(root, name) print("Subdirectory",dir_path) print("We can see a folder created succesfully with name Text_files") print("-----------------------------------------------------") #creating a new empty file into a subdirectory myfile = "favtutor.txt" new=os.path.join(dir_path,myfile) Path(new).touch() print("favtutor.txt file created inside Text_files directory") print("-----------------------------------------------------") # Try to delete the empty folder # checking for folder availaibility using try and except block try: shutil.rmtree(path) for root, dirs, files in os.walk(top=rootdir, topdown=False): if dirs==[]: print("No subdiretectory") else: print(dirs) print("We can see a folder deleted succesfully") print("-----------------------------------------------------") except OSError as e: print("Error: %s - %s." % (e.filename, e.strerror)) print("-----------------------------------------------------")
Output:
Subdirectory f:\projects\Favtutor\Text_files We can see a folder created succesfully with name Text_files ----------------------------------------------------- favtutor.txt file created inside Text_files directory ----------------------------------------------------- No subdiretectory We can see a folder deleted succesfully -----------------------------------------------------
Method 4) pathlib.Path.unlink(missing_ok=False)
The pathlib.Path.unlink() method operates on a Path object that represents the path of the file that you want to delete. If the file specified by the Path object exists, the unlink() method will delete it. If missing_ok parameter is false (default value), FileNotFoundError is raised if the path does not exist. If it is true, FileNotFoundError exceptions will be ignored.
You can check your current working directory with “Path.cwd”.
Example:
import pathlib import os path_object = Path(".") print(type(path_object)) #creating a new empty file into a subdirectory myfile = "favtutor.txt" Path(myfile).touch() print("favtutor.txt file created") print(os.listdir()) print("-----------------------------------------------------") file = pathlib.Path("favtutor.txt") file.unlink(missing_ok=False) print(os.listdir()) print("favtutor.txt file deleted")
Output:
<class 'pathlib.WindowsPath'> favtutor.txt file created ['demo.txt', 'favtutor.txt', 'jupyter.ipynb', 'SPAM text message 20170820 - Data.csv'] ----------------------------------------------------- ['demo.txt', 'jupyter.ipynb', 'SPAM text message 20170820 - Data.csv'] favtutor.txt file deleted
Method 5) pathlib.Path.rmdir()
It helps us to remove an empty directory. You need to first select the Path() for the empty directory, and then call rmdir() method on that path. It will check the folder size. If it's 0 byte i.e. empty, the will be deleted successfully else throws an error. This is a good way to delete empty folders without any fear of losing actual data.
It is similar to os.rmdir() method only way of writing the syntax is different, You can just replace the os.rmdir(dir_path) line with this function.
Path(dir_path).rmdir()
Python Problems with Removing Files
Let's now look at some of the common problems you might have with deleting files in python. Here is an Python program to delete all files with specific extensions:
import os path="Text_files" # specifiing new folder name if not os.path.exists(path): # checking for folder existance # creating a new folder os.makedirs(path) files=["python.txt","data.csv","java.txt","house_prediction.xls","C.txt"] for file in files: new=os.path.join(path+"\\"+file) # print(new) # creating 5 new files inside Text_files folder Path(new).touch() print("Files in a directory -->",os.listdir(path)) for file_name in os.listdir(path): if file_name.endswith('.txt'): os.remove(path + "\\"+file_name) print("Files deleted-->",path + "\\"+file_name) print("Files in a directory -->",os.listdir(path))
Output:
Files in a directory --> ['C.txt', 'data.csv', 'house_prediction.xls', 'java.txt', 'python.txt'] Files deleted--> Text_files\C.txt Files deleted--> Text_files\java.txt Files deleted--> Text_files\python.txt Files in a directory --> ['data.csv', 'house_prediction.xls']
Here is another python Program to Delete a file after reading its contents:
filepath="demo.txt" f = open(filepath, "r") print("contents of file") print(f.read()) print("---------------------------------------------------------------------") f.close() os.remove("demo.txt") print("File deleted succesfully")
Output:
contents of file Education in technology Though the man had been playing with science since ages, the term ‘technology’ was conceived in the early 20th century. It is inspired by the German terms “Technik” and “technologie”. Though, in German, there is a difference between ‘technik’ and ‘technologie’ the English, around the 1930s, combined both and start referring to them as “technology”. An American sociologist Read Bain gave the first definition of technology in 1937 in his journal titled “Technology and State Government”. Bain wrote that all the tools, machinery, equipment, clothes, etc are technology along with the methods we use to produce them. From time to time ‘technology’ had been defined by several scientists and engineers; nevertheless, the basic concept has always remained the same. It is a concept created by combining mental and physical effort to obtain a valuable outcome. Technology has made life easy and convenient. Before modern-day technology, carrying out everyday works was so cumbersome and difficult. It would take a whole day to just travel a couple of miles. Sending messages also wasn’t as easy as it is today. With a mobile phone which is a technological product, you can send messages instantly to the receiver on another corner of the world. A couple of centuries back, it would have taken a couple of days or even weeks. Technology has much evolved making life more easy, convenient and safe than before. Though technology is advantageous, there are also certain disadvantages attributed to it. Today, we depend on technology, so much that it would be impossible to live for a single day in absence of it. --------------------------------------------------------------------- File deleted succesfully
Also, learn how to overwrite a file in python, which is also important to learn.
Conclusion
So we covered all the different ways that python provides to delete a file from a directory and also how to check if the files exist or not. Happy Learning :)