Join Login Create a Request

How to check if a file exists in Python

You may need to read and write files in python and the first task is to check if the file even exists.

from pathlib import Path

_file = Path('/path/to/file') 
if _file.is_file(): 
    print("Exists")
else:
   print("Does not exist")

You can also use the os module to check that.

import os 
exists = os.path.isfile('/path/to/file')

Related Snippets