shell中可以通过“点”来执行任意可执行的脚本,比如PHP、Python、Ruby等。
. xxx.sh
. xxx.php
...
Python有个os.system也可以执行
os.system(". xxx.sh")
os.system(". xxx.php")
但是用subprocess如何才做呢?
.
是 shell 的 built-in 命令,subprocess 默认不使用 shell
。
关键字参数 shell 送 True,就可以了。
subprocess.Popen(". xxx.sh", shell=True)
https://docs.python.org/2.7/library/subprocess.html
subprocess.Popen('. xxx.sh', stdout = subprocess.PIPE, shell = True).stdout.read()
最后的read()
就是文件内容读取,返回字符串,如果想返回list就换成readlines()
。
然后其实用status, output = commands.getstatusoutput('. xxx.sh')
也不错,反正我更多时候会用这个。