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

PythonServer Side ProgrammingProgramming

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
raja
Published on 26-Dec-2017 06:22:26
Advertisements