Checking Path Existence and Permissions
Parameter Details
os.F_OK Value to pass as the mode parameter of access() to test the existence of path.
os.R_OK Value to include in the mode parameter of access() to test the readability of path.
os.W_OK Value to include in the mode parameter of access() to test the writability of path.
os.X_OK Value to include in the mode parameter of access() to determine if path can be executed.
Perform checks using os.access
os.access is much better solution to check whether directory exists and it’s accessible for reading and writing.
import os
path = "/home/myFiles/directory1"
Check if path exists os.access(path, os.F_OK)
Check if path is Readable os.access(path, os.R_OK)
Check if path is Writable os.access(path, os.W_OK)
Check if path is Executable os.access(path, os.E_OK)
also it's possible to perform all checks together
os.access(path, os.F_OK & os.R_OK & os.W_OK & os.E_OK)
All the above returns True if access is allowed and False if not allowed. These are available on unix and windows.
Must Read Python Interview Questions
200+ Python Tutorials With Coding Examples
Other Python Tutorials
- What is Python?
- Python Advantages
- Python For Beginners
- Python For Machine Learning
- Machine Learning For Beginners
- 130+ Python Projects With Source Code On GitHub