w3resource

Python: Check for access to a specified path

Python Operating System Services: Exercise-4 with Solution

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

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.