Remove() Function Of Os Module In Python


  • The remove() function removes a file, as specified by a given file path. The function does not work, if the file path denotes a directory.


# Example Python program that removes
# a file path from the operating system
# using the os.remove() function
import os

# File to be removed
filePath = "/ex/marked1.txt"

    # Remove the file path
    retVal = os.remove(filePath)
    print("Removed: %s"%filePath)
except FileNotFoundError:
    print("Given file path not found")


Removed: /ex/marked1.txt

If the file path /ex/marked1.txt is not available then the output will be

Given file path not found


Copyright 2023 ©