【发布时间】:2017-09-25 14:43:44
【问题描述】:
我正在使用subprocess.Popen 调用安装脚本。但是,当我通过print 将输出传送到终端时,它会丢失安装脚本输出中的任何格式。例如,所有终端颜色都消失了,下载进度条出现在多行。
这是我的代码:
process = subprocess.Popen(
[script],
shell=True,
cwd=script_dir,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env={
'WORKSPACE': build_dir
})
with process.stdout:
for line in iter(process.stdout.readline, b''):
print(line)
还有一个输出样本(通常显示为进度条):
You need to run "nvm install 4.2.2" to install it before using it.
Downloading and installing node v4.2.2...
Downloading https://nodejs.org/dist/v4.2.2/node-v4.2.2-darwin-x64.tar.gz...
0.0%
1.0%
### 4.6%
####### 10.5%
############# 18.8%
################### 26.5%
######################## 34.1%
############################## 41.9%
################################### 49.7%
######################################### 57.4%
############################################## 65.2%
#################################################### 73.0%
########################################################## 80.8%
############################################################### 88.5%
##################################################################### 96.3%
######################################################################## 100.0%
【问题讨论】:
标签: python python-2.7 subprocess