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: Unknown Arguments Using *arguments

If your function can take in any number of arguments then add a * in front of the parameter name:

def myfunc(*arguments):
 for a in arguments:
   print a
myfunc(a)
myfunc(a,b)
myfunc(a,b,c)