w3resource

Python: Check for access to a specified path


4. Path Access Checker

Write a Python program to check access to a specified path. Test the existence, readability, writability and executability of the specified path.

Sample Solution:

Python Code :

import os
print('Exist:', os.access('c:\\Users\\Public\\C programming library.docx', os.F_OK))
print('Readable:', os.access('c:\\Users\\Public\\C programming library.docx', os.R_OK))
print('Writable:', os.access('c:\\Users\\Public\\C programming library.docx', os.W_OK))
print('Executable:', os.access('c:\\Users\\Public\\C programming library.docx', os.X_OK))

Sample Output:

Exist: False
Readable: False
Writable: False
Executable: False

For more Practice: Solve these Related Problems:

  • Write a Python program that checks whether a specified path exists, and if so, tests its readability, writability, and executability using os.access().
  • Write a Python script to verify access permissions for a file path and print which permissions (read, write, execute) are granted.
  • Write a Python function that takes a file path, checks for its existence and all access permissions, and raises an error if any are missing.
  • Write a Python program to simulate a permission check on a directory path, printing a detailed report on its access status.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to scan a specified directory and identify the sub directories and files.
Next: Write a Python program to get the size, permissions, owner, device, created, last modified and last accessed date time of a specified path.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.