Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-27-2009, 03:58 PM   #1
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Python: how do you accomplish "chmod 2755 somedir" - os.chmod not working as expected

I'm trying to create a directory with permissions 2755... that's 755 with set group ID (as described in man chmod).

here's a little test to demonstrate my problem:

>>> import os
>>> os.mkdir("/tmp/testmods/a",0777)
>>> os.mkdir("/tmp/testmods/b",0755)
>>> os.mkdir("/tmp/testmods/c",2777)
>>> os.mkdir("/tmp/testmods/d",2755)
>>> os.umask(0)
>>> os.mkdir("/tmp/testmods/e",2755)
>>> os.umask(777)
>>> os.mkdir("/tmp/testmods/f",2755)
>>> os.umask(0777)
>>> os.mkdir("/tmp/testmods/g",2755)
... and you can see the output it nothing like I'd expect:

>ls -l /tmp/testmods/
total 28K
4.0K drwxrwsr-x 2 briank cg 4.0K 2009-01-27 12:47 a/   <~~ As I'd expect
4.0K drwxr-sr-x 2 briank cg 4.0K 2009-01-27 12:47 b/   <~~ Also good.
4.0K d-wx-ws--t 2 briank cg 4.0K 2009-01-27 12:47 c/   <~~ From here down, I'm lost.
4.0K d-wx--S--t 2 briank cg 4.0K 2009-01-27 12:47 d/
4.0K d-wx--S-wt 2 briank cg 4.0K 2009-01-27 12:48 e/
4.0K d-wx--S-wT 2 briank cg 4.0K 2009-01-27 12:48 f/
4.0K d-----S--T 2 briank cg 4.0K 2009-01-27 12:49 g/
What I'm trying to do is this:

akane:/tmp/testmods>mkdir cli
akane:/tmp/testmods>chmod 2755 !$
chmod 2755 cli
akane:/tmp/testmods>ls -ld !$
ls -ld cli
4.0K drwxr-sr-x 2 briank cg 4.0K 2009-01-27 12:52 cli/   <~~ This is what I want
What am I doing wrong in python? Rather, what do I need to do differently to get the same results?


edited to add: os.chmod() gives the same results as os.mkdir() re: permissions
Old 01-27-2009, 05:48 PM   #2
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51

For reasons unbeknownst to me, you must give the mode in octal rather than decimal. This means that ALL MODES must be preceded by a '0' if you expect them to work like you would if you were typing on the command line. I don't know why the python folk believe that someone might ever want to use a base10 number to set permissions or why they don't assume you're specifying octal when you specify a base10 number, but... that's neither here nor there.

The moral of the story is this...

os.mkdir("foo",2755) is "wrong"
os.mkdir("foo",02755) is "correct"

quotes used as "wrong" may not be wrong to the world... just to me.

so when you set the mode to '2755' you're really doing the same as "chmod 5303"
when you set the mode to '02755' you're doing the same as "chmod 2755"
1 members found this post helpful.
Old 11-29-2010, 04:03 PM   #3
Michael Scheper
LQ Newbie
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
Lightbulb Why 2755 is wrong

This is because, in Python, a literal integer that starts with a zero is assumed to be octal. Behold:

$ python
Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2755 # a decimal value
>>> 02755 # an octal value, which makes more sense when specifying permissions
>>> 1517 # the same value in decimal--you could pass this to os.mkdir to get the permissions you want, but that would make your code harder to understand
os.mkdir("/tmp/testmods/d",2755) is actually the same as os.mkdir("/tmp/testmods/d",05303), which is not what you want.

Note also that os.mkdir() takes the current umask into account. If you want to override this, use os.chmod() on the directory after you've used os.mkdir().

Michael Scheper.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
chmod command fails with "Read-only file system" huklBerry Linux - General 8 06-13-2008 04:35 PM
Logged in as "root"/Fedora 8 but get "Operation not permitted" when using "chmod etc gosunlee Linux - Newbie 7 02-10-2008 06:56 AM
Problem with "chmod" command installing java applet grandmarkai Mandriva 1 05-01-2006 07:00 PM
Why doesn't a wildcard chmod change "dot" files/directories? jht2k Linux - General 1 08-09-2004 03:31 PM
chmod u+s not working as expected clinton Linux - Newbie 3 03-19-2004 08:49 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration