mode for file created by open

Cameron Simpson cs at zip.com.au
Fri Jun 8 19:19:46 EDT 2012


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,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

It was a joke, OK?  If we thought it would actually be used, we wouldn't have
written it!     - Marc Andreessen on the creation of a <blink> tag



More information about the Python-list mailing list