chmod 777 到 python 脚本

chmod 777 to python script

正在尝试将 linux cmd 转换为 python 脚本

Linux 命令:

chmod 777 file1 file1/tmp file2 file2/tmp file3 file3/tmp

我知道 os.chmod(file, 0777) 但我不确定如何将它用于上述行。

os.chmod 将单个文件名作为参数,因此您需要遍历文件名并应用 chmod:

files = ['file1', 'file1/tmp', 'file2', 'file2/tmp', 'file3', 'file3/tmp']
for file in files:
    os.chmod(file, 0o0777)

顺便说一句,我不确定您为什么要将权限位设置为 777——这是自找麻烦。您应该选择尽可能严格的权限位。

chmod 777 将完成工作,但它比您可能想要授予该脚本的权限更多。

最好限制执行权限。

我建议:chmod +x myPthonFile.py

如果要使用pathlib,可以使用.chmod() from that library

from pathlib import Path

files = ['file1', 'file1/tmp', 'file2', 'file2/tmp', 'file3', 'file3/tmp']
for file in files:
    Path(file).chmod(0o0777)