newbie os.stat question

Michael Hudson mwh at python.net
Thu Apr 3 14:34:34 EST 2003


Ben Hutchings <do-not-spam-ben.hutchings at businesswebsoftware.com> writes:

> In article <AV_ia.7382$4P1.570905 at newsread2.prod.itd.earthlink.net>,
> Tipton Bandy wrote:
> > 
> > I've got my first python utility working pretty good, but
> > I just need help on one point.
> > 
> > I need to test a filename and return true or false based on 
> > whether the file is readable and writable by the user running
> > my utility. Oh, yes, this is on Linux:)

[...]

> os.access

is almost never what you want, because it uses the real user id, not
the effective user id.

> (But this is fairly useless, because the situation could change by
> the time you actually try to use the file.  You should just attempt
> to use the file and catch OSError exceptions:
> 
>     try:
>         f = open(filename)
>     except OSError, e:
>         if e.errno == errno.EPERM:
>            print 'Operation is not permitted'
> 
> )

This is still true, though.

Cheers,
M.

-- 
  The above comment may be extremely inflamatory. For your
  protection, it has been rot13'd twice.
                           -- the signature of "JWhitlock" on slashdot




More information about the Python-list mailing list