脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python subprocess pipe 实时输出日志的操作

python subprocess pipe 实时输出日志的操作

2021-08-09 00:53ADChen Python

这篇文章主要介绍了python subprocess pipe 实时输出日志的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

* test11.py

?
1
2
3
4
5
6
7
8
import time
print "1"
time.sleep(2)
print "1"
time.sleep(2)
print "1"
time.sleep(2)
print "1"

* test.py

import subprocess

p = subprocess.Popen("python test11.py", shell=True, stdout=subprocess.PIPE)

# None表示正在执行中

while p.poll() is None: <br>  out = p.stdout.readline() <br>  if out != "": <br>    print out

补充知识:python 通过 subprocess.Popen执行命令,重定向实时输出

执行命令

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import subprocess
import sys
 
# 常用编码
GBK = 'gbk'
UTF8 = 'utf-8'
 
# 解码方式,一般 py 文件执行为utf-8 ,cmd 命令为 gbk
current_encoding = GBK
popen = subprocess.Popen('ping www.baidu.com', shell = True,
             stdout = subprocess.PIPE,
             stderr = subprocess.PIPE,
             bufsize = 1)
out,err = popen.communicate()
print('std_out: ' + out)
print('std_err: ' + err)
print('returncode: ' + str(popen.returncode))

执行 .py文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import subprocess
import sys
 
# 常用编码
GBK = 'gbk'
UTF8 = 'utf-8'
 
current_encoding = UTF8
popen = subprocess.Popen('python D:\code\test.py',
             stdout = subprocess.PIPE,
             stderr = subprocess.PIPE,
             bufsize = 1)
out,err = popen.communicate()
print('std_out: ' + out)
print('std_err: ' + err)
print('returncode: ' + str(popen.returncode))

以上这篇python subprocess pipe 实时输出日志的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/chenadong/p/10150269.html

延伸 · 阅读

精彩推荐