LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python: how do you accomplish "chmod 2755 somedir" - os.chmod not working as expected (https://www.linuxquestions.org/questions/programming-9/python-how-do-you-accomplish-chmod-2755-somedir-os-chmod-not-working-as-expected-700306/)

BrianK 01-27-2009 02:58 PM

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:

Code:

>>> 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:

Code:

>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:

Code:

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?

Thanks

edited to add: os.chmod() gives the same results as os.mkdir() re: permissions

BrianK 01-27-2009 04:48 PM

Solved.

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"

Michael Scheper 11-29-2010 03:03 PM

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

Code:

$ 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
2755
>>> 02755 # an octal value, which makes more sense when specifying permissions
1517
>>> 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
1517

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().

Cheers,
Michael Scheper.


All times are GMT -5. The time now is 09:25 AM.