使用python检查文件的权限 [英] Checking permission of a file using python

查看:60
本文介绍了使用python检查文件的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个 python 脚本,首先要做的是,它需要在执行整个程序之前检查文件的权限.

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.

我目前正在使用 os.system("stat -c '%a'/data/lims/LI")在不使用 os.system 的情况下,有没有更好的方法来做到这一点?

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

我的实际编码是

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

我想检查一个文件的权限,它应该是一个数字......比如755、750等如果是可执行权限,则执行.

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.

推荐答案

是的,你可以使用 Python 的 os.stat(path)os.access(path) 直接,例如检查它的可执行性

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"

请参阅使用 Python 在 Linux 中检查文件权限了解更多详情.

这篇关于使用python检查文件的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆