def file_set_readonly(path, enable, follow_symlinks=True, recursive=False):
"""Apply or remove the read-only property of a given file or directory."""
st_mode = os.stat(path, follow_symlinks=follow_symlinks).st_mode
new_attr = (
(st_mode | stat.S_IREAD) & ~stat.S_IWRITE if enable
else (st_mode | stat.S_IWRITE) & ~stat.S_IREAD)
if new_attr != st_mode:
os.chmod(path, new_attr)
if recursive and stat.S_ISDIR(st_mode):
for entry in os.scandir(path):
file_set_readonly(entry.path, enable, follow_symlinks, recursive)
评论列表
文章目录