How to Check If a File Exists in Python


In this example, we will learn how to check if a file exists and then delete it in Python.

Source Code

import os

if os.path.exists('filename.py'):
    os.remove('filename.py')
else:
    print('The file does not exist.')

Output:

The file does not exist.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments