We will use Python for performing numerous operations on file and directories, i.e., test the existence of recordsdata, confirm the existence of directories, and take away the recordsdata and directories. Python offers a built-in working system (OS) module for this objective. By utilizing the OS module, we will entry the system recordsdata, directories, and we will delete them as properly. Subsequently, to carry out any operation on file or listing, first, we have to import the OS module. On this article, we are going to be taught to delete the file by utilizing Python.
Delete or take away a file
The os module offers a built-in os.take away() perform to take away or delete a file from the system. To delete all the folder or listing, we will use the os.rmdir() perform.
Let’s see an instance of deleting the file.
To delete a file, first, we have to embrace the os module. The os module incorporates the os.take away() perform. The os.take away() perform takes the trail of the file as a parameter. It searches for the file on the given path and removes it from the system. Let’s write a easy program to delete or take away a file.
#importing the os module
import os
#utilizing os.take away() perform to delete the file
os.take away(“/residence/linuxhint/Paperwork/check.txt”) # specifying the trail of the file
Output
Earlier than executing this system, the check.txt file is current within the Paperwork listing.
Now let’s execute our program.
The file is efficiently deleted from the Paperwork listing.
If we attempt to delete a file that doesn’t exist or is already deleted, then the Python interpreter will present an error “FileNotFoundError.” Let’s execute our program once more and attempt to delete the “check.txt” file, which is already deleted.
Output
The output is displayed on the Python console. Within the output, it may be seen that the Python interpreter throws an error “FileNotFoundError” after we attempt to delete the file that doesn’t exist.
One of the best ways to keep away from this error is that first, we should always test if the file exists, then we are going to delete it; in any other case, we are going to print a message that “File doesn’t exist.” To test the existence of the file, we will use os.path.exists() and os.path.isfile() capabilities. Let’s write a easy program to test the existence of the file first and delete the file.
On this program, we’re utilizing os.path.exists() perform to confirm the existence of the file.
#importing the os module
import os
# utilizing the os.path.exists() perform to test the existence of the file
if os.path.exists(“/residence/linuxhint/Paperwork/check.txt”):
os.take away(“/residence/linuxhint/Paperwork/check.txt”)
print(“File deleted efficiently”)
else:
print(“File doesn’t exist”)
Output
The output is displayed on the Python console. It may be noticed within the output that the Python interpreter doesn’t throw any error if the file doesn’t exist; somewhat, it executes the else block and prints the message on the console that “File not discovered.”
We will retailer the file path right into a separate variable and execute the identical program once more.
#importing the os module
import os
#declaring the trail variable to retailer the trail of the file
path=”/residence/linuxhint/Paperwork/check.txt”
# utilizing the os.path.exists() perform to test the existence of the file
if os.path.exists(path):
os.take away(path)
print(“File deleted efficiently”)
else:
print(“File doesn’t exist”)
Output
The output is displayed on the Python console.
Now let’s use the os.path.isfile() perform to test the existence of the file.
#importing the os module
import os
#declaring the trail variable to retailer the trail of file
path=”/residence/linuxhint/Paperwork/check.txt”
# utilizing the os.path.isfile() perform to test the existence of the file
if os.path.isfile(path):
os.take away(path)
print(“File deleted efficiently”)
else:
print(“File doesn’t exist”)
Output
The output is displayed on the Python console.
Delete or take away a listing
To delete or take away the listing, we will use the os.rmdir() perform. The os.rmdir() perform solely deletes the empty listing or folder. If the listing incorporates any subdirectories and recordsdata, and we attempt to delete it, then the Python interpreter will throw an “OSError.” Let’s see an instance of deleting the listing.
#importing the os module
import os
#declaring the trail variable to retailer the trail of the listing
path=”/residence/linuxhint/Paperwork/myFolder”
# utilizing the os.path.isdir() perform to test the existence of the listing
if os.path.isdir(path):
#utilizing rmdir() perform to delete the listing
os.rmdir(path)
print(“Listing deleted efficiently”)
else:
print(“Listing doesn’t exist”)
Output
The output is displayed on the Python console. The “myFolder” listing is empty and deleted efficiently.
Conclusion
Python is a flexible programming language. Accessing the system recordsdata and directories could be very simple in Python. Python offers a built-in OS module to carry out working system associated capabilities and operations. Eradicating a file and listing is a quite common operation of Python that we will carry out by utilizing the built-in os.take away() and os.rmdir() capabilities, respectively. This text helps the newbies to grasp the file and listing deletion course of in Python.
python disk cleanup,python check if file is older than,python how old is a file,python st_mtime,python script to delete old folders,os remove list of files,how to delete a file in linux terminal,unix delete directory,rm path to file,remove files containing string linux,delete command used to,unix rename file,python delete file wildcard,python remove file,linux find and delete files older than,find and delete files recursively linux,find and delete files windows,modifying file permissions in linux,delete all files containing string windows,linux find and delete files matching pattern,file and directory management in linux,lab 18-2 practice linux commands,linux compression utility program,device file in linux,lab 10-1 linux directory structures quizlet,command to delete two users simultaneously is,override r–r–r–,bash delete directory,bash script change directory,bash move file,bash commands cheat sheet,shell script to delete files in a directory,pyclean,remove pyc files from git,python disable __pycache__,find pyc and delete,py3clean,where are pyc files stored,python script to remove files older than 7 days,python delete file,python delete directory older than,python script to delete files based on date,python sftp remove file,how to delete data from file in python,python tempfile