How to control permission of file?

G

Grant Edwards

When one open()s a file (that doesn't exist) for writing , how
does one control that file's permissions (it's "mode" in Unix
terms).
 
K

K.S.Sreeram

Grant said:
When one open()s a file (that doesn't exist) for writing , how
does one control that file's permissions (it's "mode" in Unix
terms).

Check out 'os.open'
It returns a file descriptor, and if you need a file object you can use
'os.fdopen' on the file descriptor

Regards
Sreeram



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEpYMFrgn0plK5qqURAsmWAJ9XmpwyuHCQBTd7Kti0ZLYEgM/PWwCfXLFP
UPtBmB5uTmW6ZgbAi6uOPMo=
=NEmK
-----END PGP SIGNATURE-----
 
G

Grant Edwards

Check out 'os.open'
It returns a file descriptor, and if you need a file object you can use
'os.fdopen' on the file descriptor

Thanks. I thought maybe there was a less arcane way to do it.
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

Grant said:
When one open()s a file (that doesn't exist) for writing , how
does one control that file's permissions (it's "mode" in Unix
terms).

what do you mean by "contor file's mode"?

usually you try to open and if you are not allowed
you will get the exception
.... f = file("/etc/shadow")
.... print f.read()
.... except IOError, e:
.... print e
....
[Errno 13] Permission denied: '/etc/shadow'
if you want to know more about file attributes
use os.stat and constants from stat module
>>> import os
>>> os.stat("/etc/shadow") (33184, 245390L, 771L, 1, 0, 15, 604L, 1151702662, 1149675585, 1149675585)
>>>
>>> import stat
>>> stat.ST_SIZE 6
>>> os.stat("/etc/shadow")[stat.ST_SIZE] 604L
>>>

http://docs.python.org/lib/module-stat.html

hth, Daniel
 
G

Grant Edwards

what do you mean by "contor file's mode"?

Are you asking what a file's mode is?

Under Unix, it's a bitmapped value that determines what the
access permissions are for the file. There are individual bits
that enable permissions for user-read, user-write,
user-execute, group-read, group-write, group-execute,
other-read, other-write, other-execute, etc.

If you look at os.open() there's a "mode" parameter (the same
as the mode parameter in Unix's libc open()). I wanted to know
how to control a file's mode when it was created by the builtin
open().

I'm afraid I don't know how else to say it.
usually you try to open and if you are not allowed you will
get the exception
... f = file("/etc/shadow")
... print f.read()
... except IOError, e:
... print e
...
[Errno 13] Permission denied: '/etc/shadow'

True, but I don't see what it has to do with my question.
if you want to know more about file attributes

Um, thanks. I know all about file attributes.
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

True, but I don't see what it has to do with my question.

my mistake, I misunderstood your question

as Sreeram said, os.open can be used


help(os.open)

Help on built-in function open:

open(...)
open(filename, flag [, mode=0777]) -> fd

Open a file (for low level IO).



ls -l secret
-r-------- 1 root root 17 2006-07-01 00:05 secret


Regards, Daniel
 

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

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top