在MacOS上使用python,我想打开一个文件进行写入,并在其中放入一些shell命令。稍后在终端运行。

with open("my_script.sh", "w") as fd:
    fd.write("#!/bin/sh\n")
    fd.write("echo $PATH\n")

这将创建该文件,但我无法确定如何设置执行位,因此当我在终端中运行它时,将无法获得:
sh: my_script.sh: Permission denied

最佳答案

import os
os.chmod("my_script.sh", 0744)

但是要正确地选择这个值。有些值可能不安全。

关于python - 使用python为文件设置执行位,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14104778/

10-16 23:27