[Tutor] How to get file permissions (Python 2.4)?

Martin A. Brown martin at linux-ip.net
Wed Mar 19 18:29:30 CET 2014


Hi there,

 : I can use os.chmod('/usr/local/somefile') to change permissions 
 : but I haven't been able to find a way to test for file modes.
 : 
 : >>> stat.S_IRWXU('/usr/bin/python')
 : Traceback (most recent call last):
 :   File "<stdin>", line 1, in ?
 : TypeError: 'int' object is not callable
 : 
 : What should I be looking for? Python 2.4.3 (RHEL 5)

You are using the stat module [0].  You need to get the stat info 
separately.  Here's the first paragraph from the stat module docs:

  The stat module defines constants and functions for interpreting 
  the results of os.stat(), os.fstat() and os.lstat() (if they 
  exist). For complete details about the stat(), fstat() and 
  lstat() calls, consult the documentation for your system.

This looks a bit ugly and error-prone to me, but you could do 
something like this:

  stat.S_IRWXU & os.stat('/usr/bin/python').st_mode

Better yet?  Let os.access() [1] do the bitmath for you:

  os.access('/usr/bin/python', os.X_OK)

Good luck,

-Martin

 [0] http://docs.python.org/2/library/stat.html
 [1] http://docs.python.org/2/library/os.html#os.access

-- 
Martin A. Brown
http://linux-ip.net/


More information about the Tutor mailing list