0

I'm using pathlib now, which makes many things easier for me. But I'm still missing the os.access method. Is there a way to work around with pathlib?

from os import access, R_OK
from pathlib import Path



def check_access(path):
    return access(path, R_OK)

path='path'

if check_access(path):
    print("Path is available")

else:
    print("Path isn't available")
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • Does this answer your question? [How to check folder / file permissions with Pathlib](https://stackoverflow.com/questions/58534564/how-to-check-folder-file-permissions-with-pathlib) – mkrieger1 Jul 04 '20 at 11:56
  • No, cause the goal of this question is a pathlib solution. This shows what I'm actually doing. – Thingamabobs Jul 04 '20 at 11:58
  • The goal of the other question is also a pathlib solution. It’s the same question. Sorry, the auto-generated comment when voting to close as duplicate isn’t very good. – mkrieger1 Jul 04 '20 at 12:31
  • "auto-generated comment"? Do you find an answer in the link you posted that reached the goal? – Thingamabobs Jul 04 '20 at 12:42
  • My first comment was auto-generated when I voted to close the question as duplicate. I did so because it is the same question. Apparently nobody has written an answer yet which you find satisfactory, but that doesn’t change the fact that this question already exists and should not be duplicated. – mkrieger1 Jul 04 '20 at 12:46
  • Do I refer in my question about "The casting to str() is kinda ugly." like the OP in your linked question? – Thingamabobs Jul 04 '20 at 12:48
  • There are methods `exists`, `is_file` etc. Do they not do what you want to achieve? – mkrieger1 Jul 04 '20 at 12:50
  • What about the X_OK? – Thingamabobs Jul 04 '20 at 12:51

1 Answers1

0

Based on the information out of the links below I created this code:

from pathlib import Path

def check_access(path, mode=None):
    print(oct(path.stat().st_mode))
    stat = oct(path.stat().st_mode)[2:]
    if len(stat) <= 5:
        stat = f'0{stat}'
        print(stat)
    per_user  = stat[3]
##    per_group = stat[4]
##    per_other = stat[5]


    if mode == 'F_OK':
        return path.exists()
    #is it equally to 040for dir 100 for file?
    if mode == 'R_OK':
        if per_user >='4':
            return True
    if mode == 'W_OK':
        if per_user == 2 or 6 or 7:
            return True
    if mode == 'X_OK':
        if int(stat) % 2:
            return True

path = Path('directory')

if check_access(path, 'W_OK'):
    print("Path is available")

else:
    print("Path isn't available")

May someone knows a better way to solve this issue with pathlib or can teach me a better understanding of the stat method.

http://permissions-calculator.org/

https://en.wikipedia.org/wiki/File_system_permissions

Better assertEqual() for os.stat(myfile).st_mode

How can I get the Unix permission mask from a file? https://docs.python.org/3/library/pathlib.html#pathlib.Path.stat

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • This assumes owner of the file is the owner of the python process executing the code. I think first need to check check if `os.getuid()` is same as `path.stat_uid()` and corresponding permission. If not then check if `os.getuid()` belongs to `path.stat_gid()` and accordingly group permission. If both of these fails then check other access rights. – tachyon Jun 01 '21 at 14:01
  • @tachyon You cant get the *user id* with pathlib, but you could find out whos is the owner or to which group this file belongs. with `Path.owner()` and `Path.group()`. So an exclusive solution with pathlib for this case dosent seem possible to me. – Thingamabobs Jun 01 '21 at 14:18