w3resource

Python: Check for access to a specified path

Python Operating System Services: Exercise-4 with Solution

Write a Python program to check for 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 Python skills with w3resource's quiz



Python: Tips of the Day

Python: Unpack a Tuple

>>> items = (0, 'b', 'one', 10,  11, 'zero')
>>> a, b, c, d, e, f = items
>>> print(f)
zero
>>> a, *b, c = items
>>> print(b)
['b', 'one', 10, 11]
>>> *_, a, b = items
>>> print(a)
11