Python Files – Remove/Delete an existing File with Examples

Files in Python:

Python file handling is a way of saving program output to a file or reading data from a file. File handling is a crucial concept in the programming world. File management is used in almost every form of project. Assume you are constructing an inventory management system. You have data in the inventory management system related to sales and purchases, thus you must save that data somewhere. Using Python’s file management, you can save that data to a file. You must be given data in the form of a comma-separated file or a Microsoft Excel file if you wish to conduct data analysis. Using file handling, you can read data from a file and write output back into it.

Remove an existing File in Python:

To remove or delete an existing file, we utilize the “remove()” method of the “os” module. Hence we must import the “os” module to access the “remove()” method.

Syntax:

os.remove(filename)

Parameters

filename: It is the file to be deleted.

Files – Remove/Delete an existing File with Examples in Python

Example1

Approach:

  • Import os module using the import keyword.
  • Pass some random filename as an argument to the remove() function of the os module to delete that file.
  • The Exit of the Program.

Below is the implementation:

# Import os module using the import keyword
import os
# Pass some random filename as an argument to the remove() function of the
# os module to delete that file.
os.remove("samplefile.txt")

Explanation:

The file "samplefile.txt" gets deleted from the memory.

Example2: Checking whether the file exists or not before removing

When you try to access a file that does not exist it raises a FileNotFoundError. Hence we can handle that exception using try-except blocks.

To avoid an error, you need to first check if the file exists or not before attempting to delete it.

Approach:

  • Import os module using the import keyword.
  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Check if the file “samplefile.txt” exists or not using the os.path.exists() function by passing the given file name as an argument to it.
  • If the file exists, then remove that file using the remove() function of the os module.
  • Else print “sorry, there is no such file”(random text).
  • The Exit of the Program.

Below is the implementation:

# Import os module using the import keyword
import os
# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Check if the file "samplefile.txt" exists or not using the os.path.exists() function
# by passing the given file name as an argument to it.
if os.path.exists(givenFilename):
  # If the file exists, then remove that file using the remove() function of the os module
  os.remove(givenFilename)
else:
  # Else print "sorry, there is no such file"(random text).
  print("sorry, there is no such file")

Output:

sorry, there is no such file

Example3

Approach:

  • Import os module using the import keyword.
  • Pass the folder name to be deleted as an argument to the rmdir() function of the os module to delete it.
  • The Exit of the Program.

Below is the implementation:

# Import os module using the import keyword
import os
# Pass the folder name to be deleted as an argument to the rmdir() function of the os module 
# to delete it.
os.rmdir("samplefolder")

Explanation:

The folder "samplefolder" gets deleted from the memory.

Google Colab Images:

Files and Code: