[Tutor] permissions reading of a file

Kristoffer Erlandsson krier115@student.liu.se
Fri May 16 03:31:02 2003


On Thu, May 15, 2003 at 09:32:08PM -0700, Sean 'Shaleh' Perry wrote:
> On Thursday 15 May 2003 10:54, Kirk Bailey wrote:
> > I know how to SET permissions on a file; how do I READ the existing
> > permissions on an already existing file?
> 
> os.stat()

os.stat doesn't seem to return the permissions. However, os.access
checks them:

$ ls -l desktop.jpg
-rw-rw-r--    1 erl      erl        533534 Apr  8 15:06 desktop.jpg

>>> import os
>>> os.access('/home/erl/desktop.jpg', os.R_OK)
1
>>> os.access('/home/erl/desktop.jpg', os.W_OK)
1
>>> os.access('/home/erl/desktop.jpg', os.W_OK|os.R_OK)
1
>>> os.access('/home/erl/desktop.jpg', os.X_OK)
0

The modes you can test for is R_OK (is the file readible?), W_OK
(writable?), X_OK (executable?) and F_OK (does the file exist?). You
just combine these using inclusive or (|).

HTH

-- 
Kristoffer Erlandsson
E-mail:  krier115@student.liu.se
ICQ#:    378225