forked from nanowell/Brainstorm-science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck if the user has the access.py
47 lines (35 loc) · 1.12 KB
/
Check if the user has the access.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Check if the user has the access to the file or directory
import os
import sys
import stat
def main():
if len(sys.argv) != 2:
print("Usage: {} <file or directory>".format(sys.argv[0]))
sys.exit(1)
path = sys.argv[1]
print("Checking {}".format(path))
try:
mode = os.stat(path).st_mode
except FileNotFoundError:
print("File or directory not found")
sys.exit(1)
if stat.S_ISDIR(mode):
print("{} is a directory".format(path))
elif stat.S_ISREG(mode):
print("{} is a regular file".format(path))
else:
print("{} is a special file".format(path))
if os.access(path, os.R_OK):
print("{} is readable".format(path))
else:
print("{} is not readable".format(path))
if os.access(path, os.W_OK):
print("{} is writable".format(path))
else:
print("{} is not writable".format(path))
if os.access(path, os.X_OK):
print("{} is executable".format(path))
else:
print("{} is not executable".format(path))
if __name__ == "__main__":
main()