Computer >> Computer tutorials >  >> Programming >> Python

How to check if a file exists or not using Python?


You can use os.access(path, mode) to check the file permissions and existence with modes for reading, writing and execution permissions.

For example

>>> import os
>>> os.access('my_file', os.F_OK) # Check for existence of file
True
>>> os.access('my_file', os.R_OK) # Check for read access
True
>>> os.access('my_file', os.W_OK) # Check for write access
True
>>> os.access('my_file', os.X_OK) # Check for execution access
False