File Permissions

V

VJ

Hi All

I need to get the user permission of a file using python. I was trying
the following code which i found on google grups

st = os.stat(myfile)
mode = st[stat.ST_MODE]
if mode & stat.ST_IREAD:
print "readable"
if mode & stat.ST_IWRITE:
print "writable"
if mode & stat.ST_IEXEC:
print "executable"

I am getting a error saying

" Traceback (most recent call last):
File "./test.py", line 23, in ?
if mode & stat.ST_IREAD:
AttributeError: 'module' object has no attribute 'ST_IREAD' "

any idea how to resolve this error ??

Basically i want to write into a file .If the permissions are not there
then print a error message.
How do i achive this ???

Thanks,
VJ
 
S

Sybren Stuvel

VJ enlightened us with:
Basically i want to write into a file .If the permissions are not
there then print a error message. How do i achive this ???

f = file('somefile', 'w')

then catch the exception that's thrown when it can't be done.

Sybren
 
S

Sebastjan Trepca

Those constants are in stat module so add "import stat" before the program.
 
J

Juho Schultz

VJ said:
Hi All

Basically i want to write into a file .If the permissions are not there
then print a error message.
How do i achive this ???

Thanks,
VJ

One way would be a try-except block, and leave the permission checking
error message generation, etc. to the operating system.

try:
outfile = file(outfilename,"w")
except IOError, errormsg
print "Could not write to file %s: %s" % (outfilename, errormsg)
 
S

Sybren Stuvel

Sebastjan Trepca enlightened us with:
Those constants are in stat module so add "import stat" before the
program.

Yeah, but just opening the file is more Pythonic than first checking
if it can be opened in the first place.

Sybren
 
F

Fredrik Lundh

VJ said:
I need to get the user permission of a file using python. I was trying
the following code which i found on google grups

st = os.stat(myfile)
mode = st[stat.ST_MODE]
if mode & stat.ST_IREAD:
print "readable"
if mode & stat.ST_IWRITE:
print "writable"
if mode & stat.ST_IEXEC:
print "executable"

I am getting a error saying

" Traceback (most recent call last):
File "./test.py", line 23, in ?
if mode & stat.ST_IREAD:
AttributeError: 'module' object has no attribute 'ST_IREAD' "

any idea how to resolve this error ??

fix your spelling:
Help on module stat:
....

S_IREAD = 256
S_IRGRP = 32
S_IROTH = 4
S_IRUSR = 256
....
Basically i want to write into a file .If the permissions are not there
then print a error message.
How do i achive this ???

that's an entirely different thing: if you want open a file and have that
operation fail if the file doesn't have the right permissions, open the file
and deal with any error you get:

try:
f = open(filename, mode)
except IOError, v:
... cannot open the file ...

or

try:
f = open(filename, mode)
except IOError, v:
import errno
if v.errno == errno.EPERM:
... wrong permissions ...
else:
raise # some other error; propagate

(note that there's nothing that guarantees that the permissions won't
change between a stat and a subsequent open, so the "look before you
leap" approach doesn't really work for operations against the file system)

</F>
 
C

Christos Georgiou

One way would be a try-except block, and leave the permission checking
error message generation, etc. to the operating system.

try:
outfile = file(outfilename,"w")
except IOError, errormsg
print "Could not write to file %s: %s" % (outfilename, errormsg)

As a word of caution: the OP is checking for the permissions of an
*existing* file. Both Juho's and Sybren's suggestions *destroy* the file's
contents.

So, VJ, I'd suggest the following change:

Open the file for read write

outfile= open(outfilename, "r+b") # I assume binary

and later on catch errors in .write operations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top