我正在尝试在python脚本中运行bash脚本,我确信有一种方法可以实现bash脚本在python中所做的工作,但我更关注的是如何让它正常工作。在
以下是我以root用户身份运行的代码,后跟错误:
import subprocess
#variable = subprocess.check_output('dmidecode', shell=True)
#print(variable)
#run program once as root then cron it as root
try :
file = open("/var/log/serialcontrol/dmidecode.txt", "r")
except FileNotFoundError:
file = open('/var/tmp/serialcontrol.bash', 'w')
file.write("#!/bin/bash/\nif [ ! -d /var/log/serialcontrol/]\nthen\n\tmkdir /var/log/serialcontrol/\nfi");
file.close()
subprocess.call("/var/tmp/serialcontrol.bash")
这是错误
^{pr2}$
{{cd2>处理^不是可执行文件。在尝试使用
subprocess
执行它之前,请先使其可执行。在正如@anishsane在一篇评论中指出的,另一个替代
^{pr2}$chmod
-脚本的方法是这样调用它,只需删除chmod
:实际上,如果我们可以假设
bash
在PATH
上,那么这将更便于移植:相关问题 更多 >
编程相关推荐