Python:通过python代码执行shell命令

shell命令

使用shell的bc命令可以实现加法

$ echo '1 + 2' | bc
3

使用Python调用系统shell命令

os.system

返回0和1

import os

ret = os.system("echo '1 + 2' | bc")

print('ret: ', ret, type(ret))

输出

3
ret:  0 <class 'int'>

os.popen

可以获取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

参考文章

  1. python执行shell命令

猜你喜欢

转载自blog.csdn.net/mouday/article/details/131132361