Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking File Permissions in Linux with Python

Tags:

python

I'm writing a script to check permissions of files in user's directories and if they're not acceptable I'll be warning them, but I want to check permissions of not just the logged in user, but also group and others. How can i do this? It seems to me that os.access() in Python can only check the permissions for the user running the script.

like image 387
Jon Phenow Avatar asked Dec 07 '09 18:12

Jon Phenow


People also ask

How do I check permissions on a file in Python?

os. stat is the right way to get more general info about a file, including permissions per user, group, and others. The st_mode attribute of the object that os. stat returns has the permission bits for the file.

How do I check folder permissions in Python?

access function to check access: os. access('/path/to/folder', os. W_OK) # W_OK is for writing, R_OK for reading, etc.

How to check permissions of a file in Linux?

You can use os.access (path, mode) to check the file permission with modes for reading, writing and execution permissions. For example, You can also use os.stat to get the status of a file or a file descriptor.

How to check permissions for a specific user in Python script?

It seems to me that os.access () in Python can only check the permissions for the user running the script. You're right that os.access, like the underlying access syscall, checks for a specific user (real rather than effective IDs, to help out with suid situations).

What is the use of write permissions in Linux?

When files have write permissions, the user can modify (edit, delete) the file and save it. For folders, write permissions enable a user to modify its contents (create, delete, and rename the files inside it), and modify the contents of files that the user has write permissions to.

What is the difference between Reading and writing permissions?

Users that have reading permission can see the content of a file (or files in a directory). However, they cannot modify it (nor add/ remove files in a directory ). On the other hand, those who have writing privileges can edit (add and remove) files. Finally, being able to execute means the user can run the file.


2 Answers

You're right that os.access, like the underlying access syscall, checks for a specific user (real rather than effective IDs, to help out with suid situations).

os.stat is the right way to get more general info about a file, including permissions per user, group, and others. The st_mode attribute of the object that os.stat returns has the permission bits for the file.

To help interpret those bits, you may want to use the stat module. Specifically, you'll want the bitmasks defined here, and you'll use the & operator (bit-and) to use them to mask out the relevant bits in that st_mode attribute -- for example, if you just need a True/False check on whether a certain file is group-readable, one approach is:

import os import stat  def isgroupreadable(filepath):   st = os.stat(filepath)   return bool(st.st_mode & stat.S_IRGRP) 

Take care: the os.stat call can be somewhat costly, so make sure to extract all info you care about with a single call, rather than keep repeating calls for each bit of interest;-).

like image 88
Alex Martelli Avatar answered Sep 27 '22 20:09

Alex Martelli


Just to help other people like me who came here for something a bit different :

import os import stat  st = os.stat(yourfile) oct_perm = oct(st.st_mode) print(oct_perm) >>> 0o100664 //the last 3 or 4 digits is probably what you want. 

See this for more details : https://stackoverflow.com/a/5337329/1814774

like image 25
Pobe Avatar answered Sep 27 '22 18:09

Pobe