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 Code:
>ls -l /tmp/testmods/ Code:
akane:/tmp/testmods>mkdir cli Thanks edited to add: os.chmod() gives the same results as os.mkdir() re: permissions |
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" |
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 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. |