Logo
Overview
在 python 中运行 bash 实时输出结果(windows)

在 python 中运行 bash 实时输出结果(windows)

在 windows 系统下的 python 中使用环境变量运行 bash

2023年4月1日
1 min read

在 windows 的 cmd 中是不能运行 bash 的,我们需要利用 git 工具的 bash 来运行,但是用 subprocess.run()会出问题,例如不能使用已经添加到环境变量的命令,如 nvm、adb 等;因此改用 subprocess.Popen()

批量输入并且最后输出所有结果

bash_path 用于定位你的 git 工具的 bash,参数 cwd= 用于定位你的工作目录,和在文件夹中右键 git bash here 是一样的效果。

输入的命令可以带有空格,但是必须在最后加上 \n

import subprocess
bash_path = r'F:\Program Files\Git\bin\bash.exe'
subp = subprocess.Popen(bash_path,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding='utf8',cwd='$your_path')
#输入的命令可以带有空格,但是必须在最后加上`\n`
subp.stdin.write('nvm -v\n')
subp.stdin.flush()
subp.stdin.write('nvm -v\n')
subp.stdin.flush()
subp.stdin.write('nvm -v\n')
subp.stdin.flush()
#不要忘记了关闭subp,否则会阻塞
subp.stdin.close()
print(subp.stdout.read())
1.1.11
1.1.11
1.1.11

实时输入并输出结果

需要用到多线程 threading 和队列 queue 来实时获取输出结果。最后也不要忘记调用子进程的 close() 方法。

import subprocess
import queue
import threading,time
bash_path = r'F:\Program Files\Git\bin\bash.exe'
def thread_for_listen(subp,q):
for line in iter(subp.stdout.readline,''):
q.put(line)
subp = subprocess.Popen(bash_path,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding='utf8',cwd='./public')
q = queue.Queue()
p1 = threading.Thread(target =thread_for_listen,args=(subp,q))
p1.start()
for i in range(5):
subp.stdin.write('nvm -v\n')
subp.stdin.flush()
print(q.get(),end='')
time.sleep(0.3)
print('wake up after 0.3s')
subp.stdin.close()
q.task_done()
1.1.11
wake up after 0.3s
1.1.11
wake up after 0.3s
1.1.11
wake up after 0.3s
1.1.11
wake up after 0.3s
1.1.11
wake up after 0.3s