微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python原样输出linux信息

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import subprocess


# 与在命令窗口执行显示效果相同,如有彩色输出可保留,但不能返回结果
def run(command):
subprocess.call(command, shell=True)


# 实时输出但不可显示彩色,可以返回结果
def sh(command, print_msg=True):
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
lines = []
for line in iter(p.stdout.readline, b''):
line = line.rstrip().decode('utf8')
if print_msg:
print(">>>", line)
lines.append(line)
return lines


print('run():')
run("ping www.baidu.com")
print('\n\nsh():')
sh("ping www.baidu.com")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐