学而实习之 不亦乐乎

使用 python 查看文件是否是可执行

2023-10-20 07:53:06

系统环境:Linux

软件环境:Python 2.7.5,Python 3.11.2

 

通过os.access(filename,mode):

os.access(path, mode)?

Use the real uid/gid to test for access to path. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to path. mode should be F_OK to test the existence of path, or it can be the inclusive OR of one or more of R_OK, W_OK, and X_OK to test permissions. Return True if access is allowed, False if not. See the Unix man page access(2) for more information.

举例:

os.access(r"/data/a.log",os.X_OK)

#使用X_OK,来判断是否是可执行的.