1

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.

Tim
  • 103
  • 2
  • 9
  • Have you checked https://docs.python.org/2/library/os.html#os.access ? You could also use `os.stat(filename).st_mode` – Zero Apr 08 '15 at 14:30
  • @JohnGalt I think the first one is perfect for me! thank you very much! I just needed to check if it is executable or not – Tim Apr 08 '15 at 14:36
  • Possible duplicate of [Checking File Permissions in Linux with Python](https://stackoverflow.com/q/1861836/608639), [How can I get a file's permission mask?](https://stackoverflow.com/q/5337070/608639), [Checking permission of a file using python](https://stackoverflow.com/q/29517513/608639), [Check the permissions of a file in python](https://stackoverflow.com/q/27434643/608639), etc. – jww Feb 08 '18 at 08:26

1 Answers1

3

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.

Community
  • 1
  • 1
declension
  • 4,110
  • 22
  • 25