使用shell的bc命令可以实现加法
- $ echo '1 + 2' | bc
- 3
使用Python调用系统shell命令
返回0和1
- import os
-
- ret = os.system("echo '1 + 2' | bc")
-
- print('ret: ', ret, type(ret))
输出
- 3
- ret: 0 <class 'int'>
可以获取shell的返回值
- import os
-
- ret = os.popen("echo '1 + 2' | bc")
-
- print('ret: ', ret, type(ret), ret.read())
输出
- ret: <os._wrap_close object at 0x102b8b898> <class 'os._wrap_close'> 3