Checking permission of a file using python

梦想与她 提交于 2020-01-16 04:32:07

问题


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:


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.



来源:https://stackoverflow.com/questions/29517513/checking-permission-of-a-file-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!