os.mkdir and mode
Nick Craig-Wood
nick at craig-wood.com
Sat Dec 2 05:30:05 EST 2006
vj <vinjvinj at gmail.com> wrote:
> How do I do the following unix command:
>
> mkdir -m770 test
>
> with the os.mkdir command. Using os.mkdir(mode=0770) ends with the
> incorrect permissions.
You mean :-
$ python -c 'import os; os.mkdir("test", 0770)'
$ stat test/
File: `test/'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 806h/2054d Inode: 2453906 Links: 2
Access: (0750/drwxr-x---) Uid: ( 518/ ncw) Gid: ( 518/ ncw)
Access: 2006-12-02 09:42:59.000000000 +0000
Modify: 2006-12-02 09:42:59.000000000 +0000
Change: 2006-12-02 09:42:59.000000000 +0000
vs
$ rmdir test
$ mkdir -m770 test
$ stat test/
File: `test/'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 806h/2054d Inode: 2453906 Links: 2
Access: (0770/drwxrwx---) Uid: ( 518/ ncw) Gid: ( 518/ ncw)
Access: 2006-12-02 09:43:23.000000000 +0000
Modify: 2006-12-02 09:43:23.000000000 +0000
Change: 2006-12-02 09:43:23.000000000 +0000
$ umask
0022
$
So it looks like python mkdir() is applying the umask where as
/bin/mkdir doesn't. From man 2 mkdir
mkdir() attempts to create a directory named pathname.
The parameter mode specifies the permissions to use. It is modified by
the process's umask in the usual way: the permissions of the created
directory are (mode & ~umask & 0777). Other mode bits of the created
directory depend on the operating system. For Linux, see below.
So python follows C rather than shell. Seems reasonable.
To fix your problem, reset your umask thus :-
$ rmdir test
$ python -c 'import os; os.umask(0); os.mkdir("test", 0770)'
$ stat test
File: `test'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 806h/2054d Inode: 2453906 Links: 2
Access: (0770/drwxrwx---) Uid: ( 518/ ncw) Gid: ( 518/ ncw)
Access: 2006-12-02 09:48:04.000000000 +0000
Modify: 2006-12-02 09:48:04.000000000 +0000
Change: 2006-12-02 09:48:04.000000000 +0000
$
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list