472,780 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

os.mkdir and mode

vj
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.

Thanks,

VJ

Dec 2 '06 #1
8 24402
vj 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.
mkdir() works just like its C equivalent, see
http://docs.python.org/dev/lib/os-file-dir.html:

"Where it is used, the current umask value is first masked out."

Use os.chmod() after os.mkdir() to get the desired permissions.

Peter
Dec 2 '06 #2
vj <vi******@gmail.comwrote:
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 <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 2 '06 #3
Peter Otten <__*******@web.dewrote:
vj 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.

mkdir() works just like its C equivalent, see
http://docs.python.org/dev/lib/os-file-dir.html:

"Where it is used, the current umask value is first masked out."

Use os.chmod() after os.mkdir() to get the desired permissions.
I think you meant use os.umask(0) before the os.mkdir() ?

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 2 '06 #4
Nick Craig-Wood wrote:
Peter Otten <__*******@web.dewrote:
> vj 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.

mkdir() works just like its C equivalent, see
http://docs.python.org/dev/lib/os-file-dir.html:

"Where it is used, the current umask value is first masked out."

Use os.chmod() after os.mkdir() to get the desired permissions.

I think you meant use os.umask(0) before the os.mkdir() ?
No, I didn't. What is the difference/advantage of that approach?

Peter
Dec 2 '06 #5
vj
To fix your problem, reset your umask thus :-

Thanks for the detailed reply. Your fix works like a charm.

VJ

Dec 2 '06 #6
Nick Craig-Wood schrieb:
So it looks like python mkdir() is applying the umask where as
/bin/mkdir doesn't. From man 2 mkdir
Actually, mkdir(1) has no chance to not apply the umask: it also
has to use mkdir(2), which is implemented in the OS kernel, and
that applies the umask. Try

strace mkdir -m770 test

to see how mkdir solves this problem; the relevant fragment
is this:

umask(0) = 022
mkdir("test", 0770) = 0
chmod("test", 0770) = 0

So it does *both* set the umask to 0, and then apply chmod.

Looking at the source, I see that it invokes umask(0) not to
clear the umask, but to find out what the old value was.
It then invokes chmod to set any "special" bits (s, t) that
might be specified, as mkdir(2) isn't required (by POSIX spec)
to honor them.

Regards,
Martin
Dec 2 '06 #7
Peter Otten <__*******@web.dewrote:
"Where it is used, the current umask value is first masked out."

Use os.chmod() after os.mkdir() to get the desired permissions.
I think you meant use os.umask(0) before the os.mkdir() ?

No, I didn't. What is the difference/advantage of that approach?
If you use use os.umask(0) then the os.mkdir(dir, perms) will create
the directory with exactly those permissions, no chmod needed.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 4 '06 #8
Martin v. Löwis <ma****@v.loewis.dewrote:
Nick Craig-Wood schrieb:
So it looks like python mkdir() is applying the umask where as
/bin/mkdir doesn't. From man 2 mkdir

Actually, mkdir(1) has no chance to not apply the umask: it also
has to use mkdir(2), which is implemented in the OS kernel, and
that applies the umask. Try
Yes you are right of course. I didn't think that statment through did
I!
strace mkdir -m770 test

to see how mkdir solves this problem; the relevant fragment
is this:

umask(0) = 022
mkdir("test", 0770) = 0
chmod("test", 0770) = 0

So it does *both* set the umask to 0, and then apply chmod.

Looking at the source, I see that it invokes umask(0) not to
clear the umask, but to find out what the old value was.
It then invokes chmod to set any "special" bits (s, t) that
might be specified, as mkdir(2) isn't required (by POSIX spec)
to honor them.
That makes sense - the odd sequence above is one of those Unix
workarounds then...

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 4 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Salmo Bytes | last post by:
I have a script that wants to mirror a directory structure, reading from location1 and writing (mkdir) at location2. This code works fine on a my own desktop test box. But fails at 'mkdir' when...
2
by: Paul Schmidinger | last post by:
Hi, I created a directory with PHP mkdir (mode 0777). Then I try to move_ulpoaded_file() to move a file into this directory. I get an error: Warning: move_uploaded_file(): SAFE MODE...
8
by: Sue | last post by:
AccessXP in Access2000 Mode: In my code I use the MkDir method to create a folder and then I want to use the transfertext method to create a delimited text file in that folder. MkDir runs and...
1
by: joelbyrd | last post by:
I'm trying to do a mkdir(), but I'm getting permission denied error, even though I've set the mode to 0777: mkdir($full_path, 0777). I also couldn't do a chmod successfully. The code is...
5
by: eoindeb | last post by:
I am trying to create a directory on Solaris using the mkdir() function. This works fine when I pass a string literal ("/etc/hosts") to mkdir, but if I try passing a directory pointer to mkdir, it...
30
by: MikeC | last post by:
Good People, I'm writing a backup utility that uses a chdir() to go into the source directory (in which the files reside that I want to back up), so I don't want to use chdir() to get into the...
3
by: Cris | last post by:
OK, I do this call on a linux system: if(!file_exists("../pages/".$_POST."/")) { $dirname = "/home/u2/sss/sss/html/pages/".$_POST.""; mkdir($dirname, $mode); } and get this:
4
by: cclough | last post by:
Hello, I have run into a problem using the mkdir function on a windows 03 server running IIS v6 with PHP 5.0. I am trying to simply create a folder within my site. Safe Mode is set to "off" and...
2
by: _q_u_a_m_i_s's | last post by:
Hy, i encountered a weird problem on a server running php5, and apache. Seems like i cannot create folders that end with "/". for example: mkdir("test/") will fail mkdir("test") will work Is...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.