GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — os.remove() method in Python - GeeksforGeeks os.remove() method in PythonLast Updated : 26 Jun, 2026 os.remove() method is used to delete a file from the file system. It removes the specified file permanently and raises an error if the file does not exist or if the given path refers to a directory.
Example: In the code below, we delete a file and display a confirmation message.
Output
Explanation: os.remove("file.txt") deletes the file named file.txt from the current working directory. Syntax
ExamplesExample 1: In the code below, we remove a file located in the current working directory.
Output
Explanation: os.remove("data.txt") deletes the file if it exists in the current directory. Example 2: In the code below, we delete a file by specifying its complete path.
Output
Explanation: os.remove() deletes the file located at the path provided. Example 3: In the code below, we verify that a file exists before attempting to delete it.
Output
Explanation: os.path.exists(f) checks whether the file exists before calling os.remove(f), helping avoid errors. Example 4: The following example removes all files with the .log extension from the current working directory. This is useful for cleaning up log files generated by applications.
Output
Explanation: os.listdir() returns all items in the current directory. The condition f.endswith(".log") selects only .log files, and os.remove(f) deletes each matching file. Related Articles:Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |