mode for file created by open

Neal Becker ndbecker2 at gmail.com
Sat Jun 9 07:42:59 EDT 2012


Cameron Simpson wrote:

> On 08Jun2012 14:36, Neal Becker <ndbecker2 at gmail.com> wrote:
> | If a new file is created by open ('xxx', 'w')
> | 
> | How can I control the file permission bits?  Is my only choice to use chmod
> | after opening, or use os.open?
> | 
> | Wouldn't this be a good thing to have as a keyword for open?  Too bad what
> | python calls 'mode' is like what posix open calls 'flags', and what posix
> | open calls 'mode' is what should go to chmod.
> 
> Well, it does honour the umask, and will call the OS open with 0666
> mode so you'll get 0666-umask mode bits in the new file (if it is new).
> 
> Last time I called os.open was to pass a mode of 0 (raceproof lockfile).
> 
> I would advocate (untested):
> 
>   fd = os.open(...)
>   os.fchmod(fd, new_mode)
>   fp = os.fdopen(fd)
> 
> If you need to constrain access in a raceless fashion (specificly, no
> ealy window of _extra_ access) pass a restrictive mode to os.open and
> open it up with fchmod.
> 
> Cheers,

Doesn't anyone else think it would be a good addition to open to specify a file 
creation mode?  Like posix open?  Avoid all these nasty workarounds?




More information about the Python-list mailing list