473,574 Members | 2,203 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5356
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*******@yaho o.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(outfilenam e,"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**********@h elsinki.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(outfilenam e,"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(outfilenam e, "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
3146
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 when opening a file with fopen() when there is contention for the file: $fp=fopen($ctr, 'w'); //only write if we can get lock on file if...
5
6459
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 $logPath/$logFileName should delete the log file, however, it never deletes it due to permissions error. However, upon checking the file properties I find this:
0
2322
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 1&1 with only limited server configuration via a web based control panel. My query relates to the ASP security model and how it relates to...
2
3927
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 1&1 with only limited server configuration via a web based control panel. My query relates to the ASP security model and how it relates to...
15
2803
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. Everything is the default settings I believe. IIS is running under Local System. In IIS the DefaultAppPool is running under Network Service....
10
2603
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 scriptname.py but *where* do you run this? Is this done on your personal system, or on the server? What if you are using Windows? If it's done on the...
7
19056
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 have the necessary permissions. One hack could be to check for length and that would throw a FileNotFoundException ...but there is got to be a better...
0
1396
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. Obviously, this doesn't exist in IIS, so as a workaround, I have the .net isapi dll as a wildcard handler for the site, with the rewriting done by...
1
5441
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 group get automatically re-directed after uploading. However, this member group never gets the benefit of knowing if they've uploaded an incorrect...
2
2034
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 files are sitting on a linux server. I know almost nothing about linux. The background: I was given a web share by my IT admin. Initially, the web...
0
7803
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7720
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8096
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6451
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5618
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5299
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2240
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1337
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.