python os.makedirs 不分配组写权限
python os.makedirs doesn't assign group write permission
以下是重现此问题的步骤列表:
chaudhary@recsys $ ipython3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: !pwd
/tmp/test
In [2]: !ls -ltr /tmp | grep test
drwxrwxr-x 2 chaudhary recsys 4096 Jul 20 12:01 test
In [3]: import os
In [4]: os.makedirs('foo/bar/baz', mode=0o775)
In [5]: !tree -pugh
.
└── [drwxr-xr-x chaudhary recsys 4.0K] foo
└── [drwxr-xr-x chaudhary recsys 4.0K] bar
└── [drwxr-xr-x chaudhary recsys 4.0K] baz
3 directories, 0 files
理想情况下,所有这些文件夹都应具有组写入权限drwxrwxr-x
。我知道我可以按照 this question's answer.
中所述解决这个问题
我想知道是否有什么我遗漏的可能是组权限错误的原因。
更新:
上面显示的测试片段是在 Linux (Ubuntu 16.04) 上完成的。我也能够在 Mac 中复制它。
chaudhary@MacBookProoo $ mkdir /tmp/test; chmod 775 /tmp/test; ls -l /tmp/ | grep test; cd /tmp/test; python3 -c 'import os; os.makedirs("foo/bar/baz", 0o775)'; ls -l /tmp/test; ls -l /tmp/test/foo; ls -l /tmp/test/foo/bar
drwxrwxr-x 2 chaudhary wheel 68 Jul 20 12:22 test
total 0
drwxr-xr-x 3 chaudhary wheel 102 Jul 20 12:22 foo
total 0
drwxr-xr-x 3 chaudhary wheel 102 Jul 20 12:22 bar
total 0
drwxr-xr-x 2 chaudhary wheel 68 Jul 20 12:22 baz
文档中说在某些系统中模式参数被忽略或没有很好地解释:
On some systems, mode is ignored. Where it is used, the current umask
value is first masked out. If bits other than the last 9 (i.e. the
last 3 digits of the octal representation of the mode) are set, their
meaning is platform-dependent. On some platforms, they are ignored and
you should call chmod() explicitly to set them.
它建议您改用 chmod()
Python 尊重 umask
有时完全忽略该模式:
On some systems, mode is ignored. Where it is used, the current umask
value is first masked out. If bits other than the last 9 (i.e. the
last 3 digits of the octal representation of the mode) are set, their
meaning is platform-dependent. On some platforms, they are ignored and
you should call chmod() explicitly to set them.
来自:https://docs.python.org/3/library/os.html
更多关于 umask here
所以一种方法是将 umask 设置为不干扰您想要的权限(对于您的 运行 进程 - 之后一定要重置它们):
demo@demo:~/demo$ mkdir demo0
demo@demo:~/demo$ cat test.py
import os
os.makedirs("demo1/demo2/demo3",0775)
demo@demo:~/demo$ python test.py
demo@demo:~/demo$ ls -lah
total 24K
drwxr-xr-x 4 demo demo 4.0K Jul 20 06:52 .
drwxr-xr-x 6 demo demo 4.0K Jul 20 06:52 ..
drwxr-xr-x 2 demo demo 4.0K Jul 20 06:52 demo0
drwxr-xr-x 3 demo demo 4.0K Jul 20 06:52 demo1
-rw-r--r-- 1 demo demo 118 Jul 20 06:52 test2.py
-rw-r--r-- 1 demo demo 50 Jul 20 06:46 test.py
demo@demo:~/demo$ cat test2.py
import os
try:
oldumask = os.umask(0)
os.makedirs("demo2/demo3/demo4",0775)
finally:
os.umask(oldumask)
demo@demo:~/demo$ python test2.py
demo@demo:~/demo$ ls -lah
total 28K
drwxr-xr-x 5 demo demo 4.0K Jul 20 06:52 .
drwxr-xr-x 6 demo demo 4.0K Jul 20 06:52 ..
drwxr-xr-x 2 demo demo 4.0K Jul 20 06:52 demo0
drwxr-xr-x 3 demo demo 4.0K Jul 20 06:52 demo1
drwxrwxr-x 3 demo demo 4.0K Jul 20 06:52 demo2
-rw-r--r-- 1 demo demo 118 Jul 20 06:52 test2.py
-rw-r--r-- 1 demo demo 50 Jul 20 06:46 test.py
demo@demo:~/demo$ ls -lah demo2/
total 12K
drwxrwxr-x 3 demo demo 4.0K Jul 20 06:52 .
drwxr-xr-x 5 demo demo 4.0K Jul 20 06:52 ..
drwxrwxr-x 3 demo demo 4.0K Jul 20 06:52 demo3
demo@demo:~/demo$
当然,如果你一直需要ose权限,你可以在os级别设置umask。
以下是重现此问题的步骤列表:
chaudhary@recsys $ ipython3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: !pwd
/tmp/test
In [2]: !ls -ltr /tmp | grep test
drwxrwxr-x 2 chaudhary recsys 4096 Jul 20 12:01 test
In [3]: import os
In [4]: os.makedirs('foo/bar/baz', mode=0o775)
In [5]: !tree -pugh
.
└── [drwxr-xr-x chaudhary recsys 4.0K] foo
└── [drwxr-xr-x chaudhary recsys 4.0K] bar
└── [drwxr-xr-x chaudhary recsys 4.0K] baz
3 directories, 0 files
理想情况下,所有这些文件夹都应具有组写入权限drwxrwxr-x
。我知道我可以按照 this question's answer.
我想知道是否有什么我遗漏的可能是组权限错误的原因。
更新:
上面显示的测试片段是在 Linux (Ubuntu 16.04) 上完成的。我也能够在 Mac 中复制它。
chaudhary@MacBookProoo $ mkdir /tmp/test; chmod 775 /tmp/test; ls -l /tmp/ | grep test; cd /tmp/test; python3 -c 'import os; os.makedirs("foo/bar/baz", 0o775)'; ls -l /tmp/test; ls -l /tmp/test/foo; ls -l /tmp/test/foo/bar
drwxrwxr-x 2 chaudhary wheel 68 Jul 20 12:22 test
total 0
drwxr-xr-x 3 chaudhary wheel 102 Jul 20 12:22 foo
total 0
drwxr-xr-x 3 chaudhary wheel 102 Jul 20 12:22 bar
total 0
drwxr-xr-x 2 chaudhary wheel 68 Jul 20 12:22 baz
文档中说在某些系统中模式参数被忽略或没有很好地解释:
On some systems, mode is ignored. Where it is used, the current umask value is first masked out. If bits other than the last 9 (i.e. the last 3 digits of the octal representation of the mode) are set, their meaning is platform-dependent. On some platforms, they are ignored and you should call chmod() explicitly to set them.
它建议您改用 chmod()
Python 尊重 umask
有时完全忽略该模式:
On some systems, mode is ignored. Where it is used, the current umask value is first masked out. If bits other than the last 9 (i.e. the last 3 digits of the octal representation of the mode) are set, their meaning is platform-dependent. On some platforms, they are ignored and you should call chmod() explicitly to set them.
来自:https://docs.python.org/3/library/os.html
更多关于 umask here
所以一种方法是将 umask 设置为不干扰您想要的权限(对于您的 运行 进程 - 之后一定要重置它们):
demo@demo:~/demo$ mkdir demo0
demo@demo:~/demo$ cat test.py
import os
os.makedirs("demo1/demo2/demo3",0775)
demo@demo:~/demo$ python test.py
demo@demo:~/demo$ ls -lah
total 24K
drwxr-xr-x 4 demo demo 4.0K Jul 20 06:52 .
drwxr-xr-x 6 demo demo 4.0K Jul 20 06:52 ..
drwxr-xr-x 2 demo demo 4.0K Jul 20 06:52 demo0
drwxr-xr-x 3 demo demo 4.0K Jul 20 06:52 demo1
-rw-r--r-- 1 demo demo 118 Jul 20 06:52 test2.py
-rw-r--r-- 1 demo demo 50 Jul 20 06:46 test.py
demo@demo:~/demo$ cat test2.py
import os
try:
oldumask = os.umask(0)
os.makedirs("demo2/demo3/demo4",0775)
finally:
os.umask(oldumask)
demo@demo:~/demo$ python test2.py
demo@demo:~/demo$ ls -lah
total 28K
drwxr-xr-x 5 demo demo 4.0K Jul 20 06:52 .
drwxr-xr-x 6 demo demo 4.0K Jul 20 06:52 ..
drwxr-xr-x 2 demo demo 4.0K Jul 20 06:52 demo0
drwxr-xr-x 3 demo demo 4.0K Jul 20 06:52 demo1
drwxrwxr-x 3 demo demo 4.0K Jul 20 06:52 demo2
-rw-r--r-- 1 demo demo 118 Jul 20 06:52 test2.py
-rw-r--r-- 1 demo demo 50 Jul 20 06:46 test.py
demo@demo:~/demo$ ls -lah demo2/
total 12K
drwxrwxr-x 3 demo demo 4.0K Jul 20 06:52 .
drwxr-xr-x 5 demo demo 4.0K Jul 20 06:52 ..
drwxrwxr-x 3 demo demo 4.0K Jul 20 06:52 demo3
demo@demo:~/demo$
当然,如果你一直需要ose权限,你可以在os级别设置umask。