os.remove
Delete a file.
Python 3.13
os.remove(path)Parameters
- pathstr | PathLike
- The path to the file.
Return value
Nothing (None); raises FileNotFoundError if the file is missing.
Examples
Deleting a file
Python 3.13
import os
open('temp.txt', 'w').close()
os.remove('temp.txt')
print(os.path.exists('temp.txt'))False
Deleting a missing file
Python 3.13
import os
try:
os.remove('missing.txt')
except FileNotFoundError:
print('File not found')File not found
Details
The file is deleted at once and for good, without a trash bin. A directory cannot be deleted this way: os.rmdir deletes an empty directory, and shutil.rmtree deletes a directory with its contents.
