Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pathlib Path().mkdir()将所需模式应用于最终目录,但将模式umask mode应用于父目录-bug?_Python_Permissions_Parent_Mkdir_Pathlib - Fatal编程技术网

Python pathlib Path().mkdir()将所需模式应用于最终目录,但将模式umask mode应用于父目录-bug?

Python pathlib Path().mkdir()将所需模式应用于最终目录,但将模式umask mode应用于父目录-bug?,python,permissions,parent,mkdir,pathlib,Python,Permissions,Parent,Mkdir,Pathlib,我正在使用pathlib设置文件夹结构,我希望树中所有文件夹的权限都设置为drwxrwx---(770) 我目前的代码是: p=Path('name/{}/{}/{}/category'.format(year,month,day)) pp=Path('name/{}/{}/{}'.format(year,month,day)) p.mkdir(mode=0o770,parents=True,exist_ok=True) 我需要exist\u ok=True,因为我希望在循环遍历categor

我正在使用pathlib设置文件夹结构,我希望树中所有文件夹的权限都设置为drwxrwx---(770)

我目前的代码是:

p=Path('name/{}/{}/{}/category'.format(year,month,day))
pp=Path('name/{}/{}/{}'.format(year,month,day))
p.mkdir(mode=0o770,parents=True,exist_ok=True)
我需要
exist\u ok=True
,因为我希望在循环遍历
category
值时使用同一行。但是,在测试这个时,我正在删除文件夹

跑完以后,

oct(p.stat().st_mode)
0o40770
oct(pp.stat().st_mode)
0o40775
i、 例如,父目录的默认权限为777(umask=002)

我能想到的唯一解决办法是:

p.mkdir(mode=0o770,parents=True,exist_ok=True)
os.system("chmod -R 770 {}".format(name))
是否有方法通过调用
Path().mkdir()
应用所需的权限,或者
os.system()
调用不可避免?

for
Path.mkdir
提到了以下行为:

如果
parents
为true,则根据需要创建此路径的任何缺失的父级;它们是使用默认权限创建的,不考虑模式(模仿POSIX mkdir-p命令)

避免这种情况的一种方法是迭代每个路径的
部分
父路径
自己,调用
mkdir
,在每个路径上都有
存在,但没有
父路径
。这样,仍然会创建丢失的目录,但会考虑
模式
。这看起来像:

for parent in reversed(p.parents):
    parent.mkdir(mode=0o770, exist_ok=True)

杰出的需要额外的
p.mkdir(mode=0o770,exist\u ok=True)
,但我很满意。