使用Python检查Linux中的文件权限

时间:2023-01-26 22:01:55

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 .access() in Python can only check the permissions for the user running the script.

我正在编写一个脚本,以检查用户目录中的文件的权限,如果它们不能被接受,我将警告它们,但是我想检查的不仅仅是登录用户的权限,还有组和其他的权限。我该怎么做呢?在我看来,Python中的.access()只能检查运行脚本的用户的权限。

5 个解决方案

#1


83  

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).

你是对的,操作系统。访问,就像底层的访问系统一样,检查特定的用户(真实的而不是有效的id,以帮助解决suid情况)。

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.

操作系统。stat是获取关于文件的更一般信息的正确方法,包括每个用户、组和其他用户的权限。对象的st_mode属性。stat返回具有文件的权限位。

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:

为了帮助解释这些位,您可能需要使用stat模块。具体地说,您需要在这里定义位掩码,并使用&操作符(bit-and)来使用它们来屏蔽st_mode属性中的相关位——例如,如果您只需要对某个文件是否为组可读的进行True/False检查,一种方法是:

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;-).

注意:操作系统。统计电话可能有点昂贵,所以确保提取所有你关心的信息一个电话,而不是不断重复的电话为每一点的兴趣;

#2


8  

You can check file permissions via os.stat(path) in conjunction with the stat module for interpreting the results.

您可以通过os.stat(path)与stat模块一起检查文件权限,以解释结果。

#3


7  

Use os.access() with flags os.R_OK, os.W_OK, and os.X_OK.

使用os.access()和标志os。R_OK,操作系统。W_OK,os.X_OK。

Edit: Check out this related question if you are testing directory permissions on Windows.

编辑:如果您正在测试Windows上的目录权限,请检查这个相关的问题。

#4


3  

os.stat and the associated bit masks for the mode.

操作系统。stat和模式的相关位掩码。

#5


2  

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://*.com/a/5337329/1814774

详细信息请参见本文:https://*.com/a/5337329/1814774

#1


83  

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).

你是对的,操作系统。访问,就像底层的访问系统一样,检查特定的用户(真实的而不是有效的id,以帮助解决suid情况)。

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.

操作系统。stat是获取关于文件的更一般信息的正确方法,包括每个用户、组和其他用户的权限。对象的st_mode属性。stat返回具有文件的权限位。

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:

为了帮助解释这些位,您可能需要使用stat模块。具体地说,您需要在这里定义位掩码,并使用&操作符(bit-and)来使用它们来屏蔽st_mode属性中的相关位——例如,如果您只需要对某个文件是否为组可读的进行True/False检查,一种方法是:

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;-).

注意:操作系统。统计电话可能有点昂贵,所以确保提取所有你关心的信息一个电话,而不是不断重复的电话为每一点的兴趣;

#2


8  

You can check file permissions via os.stat(path) in conjunction with the stat module for interpreting the results.

您可以通过os.stat(path)与stat模块一起检查文件权限,以解释结果。

#3


7  

Use os.access() with flags os.R_OK, os.W_OK, and os.X_OK.

使用os.access()和标志os。R_OK,操作系统。W_OK,os.X_OK。

Edit: Check out this related question if you are testing directory permissions on Windows.

编辑:如果您正在测试Windows上的目录权限,请检查这个相关的问题。

#4


3  

os.stat and the associated bit masks for the mode.

操作系统。stat和模式的相关位掩码。

#5


2  

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://*.com/a/5337329/1814774

详细信息请参见本文:https://*.com/a/5337329/1814774