Python Code to Delete a File


We can use Python for performing various operations on file and directories, i.e., check the existence of files, verify the existence of directories, and remove the files and directories. Python provides a built-in operating system (OS) module for this purpose. By using the OS module, we can access the system files, directories, and we can delete them as well. Therefore, to perform any operation on file or directory, first, we need to import the OS module. In this article, we will learn to delete the file by using Python.

Delete or remove a file

The os module provides a built-in os.remove() function to remove or delete a file from the system. To delete the entire folder or directory, we can use the os.rmdir() function.

Let’s see an example of deleting the file.

To delete a file, first, we need to include the os module. The os module contains the os.remove() function. The os.remove() function takes the path of the file as a parameter. It searches for the file at the given path and removes it from the system. Let’s write a simple program to delete or remove a file.

#importing the os module
import os
#using os.remove() function to delete the file
os.remove("/home/linuxhint/Documents/test.txt") # specifying the path of the file

Output
Before executing the program, the test.txt file is present in the Documents directory.

Now let’s execute our program.

The file is successfully deleted from the Documents directory.

If we try to delete a file that does not exist or is already deleted, then the Python interpreter will show an error “FileNotFoundError.” Let’s execute our program again and try to delete the “test.txt” file, which is already deleted.

Output
The output is displayed on the Python console. In the output, it can be seen that the Python interpreter throws an error “FileNotFoundError” when we try to delete the file that does not exist.

The best way to avoid this error is that first, we should check if the file exists, then we will delete it; otherwise, we will print a message that “File does not exist.” To check the existence of the file, we can use os.path.exists() and os.path.isfile() functions. Let’s write a simple program to check the existence of the file first and delete the file.

In this program, we are using os.path.exists() function to verify the existence of the file.

#importing the os module
import os
# using the os.path.exists() function to check the existence of the file
if os.path.exists("/home/linuxhint/Documents/test.txt"):
   os.remove("/home/linuxhint/Documents/test.txt")
   print("File deleted successfully")
else:
   print("File does not exist")

Output
The output is displayed on the Python console. It can be observed in the output that the Python interpreter does not throw any error if the file does not exist; rather, it executes the else block and prints the message on the console that “File not found.”

We can store the file path into a separate variable and execute the same program again.

#importing the os module
import os
#declaring the path variable to store the path of the file
path="/home/linuxhint/Documents/test.txt"
# using the os.path.exists() function to check the existence of the file
if os.path.exists(path):
   os.remove(path)
   print("File deleted successfully")
else:
   print("File does not exist")

Output
The output is displayed on the Python console.

Now let’s use the os.path.isfile() function to check the existence of the file.

#importing the os module
import os
#declaring the path variable to store the path of file
path="/home/linuxhint/Documents/test.txt"
# using the os.path.isfile() function to check the existence of the file
if os.path.isfile(path):
   os.remove(path)
   print("File deleted successfully")
else:
   print("File does not exist")

Output
The output is displayed on the Python console.

Delete or remove a directory

To delete or remove the directory, we can use the os.rmdir() function. The os.rmdir() function only deletes the empty directory or folder. If the directory contains any subdirectories and files, and we try to delete it, then the Python interpreter will throw an “OSError.” Let’s see an example of deleting the directory.

#importing the os module
import os
#declaring the path variable to store the path of the directory
path="/home/linuxhint/Documents/myFolder"
# using the os.path.isdir() function to check the existence of the directory
if os.path.isdir(path):
   #using rmdir() function to delete the directory
   os.rmdir(path)
   print("Directory deleted successfully")
else:
   print("Directory does not exist")

Output
The output is displayed on the Python console. The “myFolder” directory is empty and deleted successfully.

Conclusion

Python is a versatile programming language. Accessing the system files and directories is very easy in Python. Python provides a built-in OS module to perform operating system related functions and operations. Removing a file and directory is a very common operation of Python that we can perform by using the built-in os.remove() and os.rmdir() functions, respectively. This article helps the beginners to understand the file and directory deletion process in Python.



from Linux Hint https://ift.tt/3ocPKox

Post a Comment

0 Comments