Remove() Function Of Os Module In Python

Overview:

  • 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:

# 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"

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

Output:

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 © pythontic.com