Checking permission of a file using python

2019-08-15 02:32发布

I am running a python script and very first thing to do is, it needs to check a permission of a file before it executes the whole program.

I am currently using os.system("stat -c '%a' /data/lims/LI") is there a better way to do this without using os.system?

my actual coding is

checker = int(os.system("stat -c '%a' /data/lims/LI"))
           if checker != 000:
                  print "pass"

I want to check the permission of a file which should be a number... something like 755, 750, and others if it is a executable permission, than execute.

1条回答
何必那么认真
2楼-- · 2019-08-15 03:24

Yes, you can use Python's os.stat(path) or os.access(path) directly, e.g. to check it's executable

if os.access("/data/lims/LI", os.X_OK):
     print "pass"

See Checking File Permissions in Linux with Python for more details.

查看更多
登录 后发表回答