对我没有用:
target_dir = "a/b/c/d/e/"
os.makedirs(target_dir,0777)
os.chmod work only for last directory ...
对我没有用:
target_dir = "a/b/c/d/e/"
os.makedirs(target_dir,0777)
os.chmod work only for last directory ...
os.walk
来遍历目录。(以下内容未经过测试,请自行尝试)for r, d, f in os.walk(path):
os.chmod(r, 0o777)
ghostdog74的答案几乎可行,但它在chmod之前尝试进入目录。所以真正的答案不太优雅:
os.chmod(path , 0o777)
for root,dirs,_ in os.walk(path):
for d in dirs :
os.chmod(os.path.join(root,d) , 0o777)
这个的一行版本是:
list(map(lambda x: os.chmod(x[0], 0o775), os.walk(target_dir)))
当你需要使用Python控制台进行这些更改时,使用更易读的for
循环版本可能更适合在生产代码中使用。
[os.chmod(root, 0o755) for root, dirs, files in os.walk(target_dir)]
。 - Kyuuhachi
topdown
默认为True
。我担心它会尝试遍历一个没有权限的目录。 - Adrian Ratnapala