Chmod recursively directories only?

匿名 (未验证) 提交于 2019-12-03 02:27:02

问题:

It's not work fo me:

target_dir = "a/b/c/d/e/" os.makedirs(target_dir,0777)  

os.chmod work only for last directory ...

回答1:

You can use os.walk to traverse directories. (Below not tested, experiment it yourself)

for r, d, f in os.walk(path):     os.chmod(r, 0o777) 


回答2:

ghostdog74's answer almost works, but it tries to walk into the directory before it chmods it. So the real answer is less elegant:

os.chmod(path , 0o777) for root,dirs,_ in os.walk(path):     for d in dirs :         os.chmod(os.path.join(root,d) , 0o777) 


回答3:

One line version for this is:

list(map(lambda x: os.chmod(x[0], 0o775), os.walk(target_dir))) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!