473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File Permissions

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

Mar 10 '06 #1
6 5351
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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Mar 10 '06 #2
Those constants are in stat module so add "import stat" before the program.

On 10 Mar 2006 06:20:18 -0800, VJ <vi*******@yahoo.com> wrote:
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

--
http://mail.python.org/mailman/listinfo/python-list

Mar 10 '06 #3
VJ wrote:
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)

Mar 10 '06 #4
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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Mar 10 '06 #5
"VJ" wrote:
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(stat)

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>

Mar 11 '06 #6
On Fri, 10 Mar 2006 16:43:15 +0200, rumours say that Juho Schultz
<ju**********@helsinki.fi> might have written:
VJ wrote:
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)


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.
--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians
Mar 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required...
5
by: Phil Powell | last post by:
print_r(is_file("$logPath/$logFileName")); // RETURNS 1 unlink("$logPath/$logFileName"); // RETURNS WARNING: PERMISSION DENIED This code should tell me that the file located at...
0
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
2
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
15
by: David Thielen | last post by:
Hi; My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a subdirectory to display as part of the created page. However, the bitmap will not display due to a security violation. ...
10
by: John Salerno | last post by:
I always read about how you need to set certain file permissions (for cgi files, for example), but it's never been clear to me *how* you do this. I know you can run the line chmod 755...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
0
by: Tim Payne | last post by:
I have an odd permissions issue with uploading files to a windows 2003 server. I have a reasonably unusual setup. We have a php website, running through IIS that was written to use mod_rewrite....
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
2
by: beary | last post by:
Hello everyone, I posted this in unix/linux but it received no replies, so I assume it was the wrong forum. I'm trying here. I'm in way over my head with file permissions. The directory and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.