python调用shell脚本
假定存在一个shell脚本:t.sh,
#!/bin/bash
echo "Hell world!"
exit 2
设置该文件权限为所有用户可执行。
调用shell脚本
>>> import os
>>> os.system("t.sh")
Hello world!
512
调用带有输出结果的命令
>>> import os
>>> file = os.popen("t.sh")
>>> file.read()
>>> 'hello world!\n'
使用 subprocess命令
>>> import subprocess
>>> subprocess.call('ls -l', shell=True)
-rwxr-xr-x 1 root root 39 Dec 11 10:22 t.sh
0
还没有评论,来说两句吧...