扫码关注官方订阅号
shell中可以通过“点”来执行任意可执行的脚本,比如PHP、Python、Ruby等。
. xxx.sh . xxx.php ...
Python有个os.system也可以执行
os.system(". xxx.sh") os.system(". xxx.php")
但是用subprocess如何才做呢?
https://docs.python.org/2.7/library/subprocess.html
. 是 shell 的 built-in 命令,subprocess 默认不使用 shell。
.
shell
关键字参数 shell 送 True,就可以了。
subprocess.Popen(". xxx.sh", shell=True)
subprocess.Popen('. xxx.sh', stdout = subprocess.PIPE, shell = True).stdout.read()
最后的read()就是文件内容读取,返回字符串,如果想返回list就换成readlines()。
read()
readlines()
然后其实用status, output = commands.getstatusoutput('. xxx.sh')也不错,反正我更多时候会用这个。
status, output = commands.getstatusoutput('. xxx.sh')
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
https://docs.python.org/2.7/library/subprocess.html
.是 shell 的 built-in 命令,subprocess 默认不使用shell。关键字参数 shell 送 True,就可以了。
最后的
read()就是文件内容读取,返回字符串,如果想返回list就换成readlines()。然后其实用
status, output = commands.getstatusoutput('. xxx.sh')也不错,反正我更多时候会用这个。