2025年4月12日 星期六 乙巳(蛇)年 正月十三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

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

时间:05-24来源:作者:点击数:36

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
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门