开发者

Chmod recursively directories only?

开发者_运维知识库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 ...


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)


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)


One line version for this is:

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

It is helpful when you have to use python console to make these changes, probably better to use the more readable for loop version suggested above in production code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜