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

Alan Gauld alan.gauld at btinternet.com
Thu Mar 20 02:13:28 CET 2014


On 19/03/14 16:48, leam hall wrote:
> 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)

v2.4? Hmm, old. I think this is still valid tho'....

Look in the os.path module for a bunch of mode/access
test functions for common tests.

Or

os.access() takes a filename and flag variable (one of os.F_OK,
os.R_OK, os.W_OK, and os.X_OK) which returns a Boolean result depending 
on whether the file exists, is readable, writable or executable 
respectively.

Alternatively use os.stat() and treat the mode attribute
result as a bitmask using the stat flags

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list