如何强制Python的打印功能输出到屏幕?
这不是禁用输出缓冲的重复- 链接的问题是尝试无缓冲输出,而这是更一般的.这个问题的最佳答案太强大或涉及到这个问题(他们不是很好的答案),这个问题可以在谷歌上找到一个相对新手.
Python的解释器默认启用输出缓冲sys.stdout吗?
如果答案是肯定的,那么禁用它的所有方法是什么?
建议到目前为止:
-u命令行开关sys.stdout在每次写入后刷新的对象PYTHONUNBUFFEREDenv varsys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)是否有任何其他方式来设置一些全局标志sys/ sys.stdout程序执行过程中?
我正在尝试为命令行程序(svnadmin verify)编写一个包装器脚本,它将为操作显示一个很好的进度指示器.这要求我能够在输出后立即查看包装程序的每一行输出.
我想我只是执行程序使用subprocess.Popen,使用stdout=PIPE,然后读取每一行,并相应地对其进行操作.但是,当我运行以下代码时,输出似乎在某处缓冲,导致它出现在两个块中,第1行到第332行,然后是333到439(输出的最后一行)
from subprocess import Popen, PIPE, STDOUT
p = Popen('svnadmin verify /var/svn/repos/config', stdout = PIPE,
stderr = STDOUT, shell = True)
for line in p.stdout:
print line.replace('\n', '')
Run Code Online (Sandbox Code Playgroud)
稍微查看子进程的文档后,我发现bufsize参数为Popen,所以我尝试将bufsize设置为1(缓冲每行)和0(无缓冲区),但这两个值似乎都没有改变行的传递方式.
此时我开始掌握吸管,所以我编写了以下输出循环:
while True:
try:
print p.stdout.next().replace('\n', '')
except StopIteration:
break
Run Code Online (Sandbox Code Playgroud)
但得到了相同的结果.
是否有可能获得使用子进程执行的程序的"实时"程序输出?Python中是否有其他选项可以向前兼容(不是exec*)?
我正在使用Python subprocess.communicate()来从一个运行大约一分钟的进程中读取stdout.
如何stdout以流式方式打印出该流程的每一行,以便我可以看到生成的输出,但在继续之前仍然阻止流程终止?
subprocess.communicate() 似乎立刻提供所有输出.
Ipython Jupyter Notebook中有一个有趣的选项可以直接从笔记本中执行命令行语句.例如:
! mkdir ...
! python file.py
Run Code Online (Sandbox Code Playgroud)
此外 - 此代码可以使用os以下命令运行:
import os
os.system('cmd command')
Run Code Online (Sandbox Code Playgroud)
但是如何运行交互式shell命令.例如:
!conda install package
Run Code Online (Sandbox Code Playgroud)
可能需要将来的input([Y]/N)或文件夹位置,但不接受进一步的输入.
我需要运行一个程序并将其输出收集到stdout.这个程序(socat)需要在python脚本的持续时间内在后台运行.一旦它运行,Socat就会处于dameon模式,但首先它会向stdout输出一些我需要用于其余脚本的行.
命令: socat -d -d PTY: PTY:
输出:
2011/03/23 21:12:35 socat[7476] N PTY is /dev/pts/1
2011/03/23 21:12:35 socat[7476] N PTY is /dev/pts/2
2011/03/23 21:12:35 socat[7476] N starting data transfer loop with FDs [3,3] and [5,5]
Run Code Online (Sandbox Code Playgroud)
...
我基本上想在我的程序开始时运行它并让它一直运行直到脚本终止,但我需要将两个/ dev/pts/X名称读入python.
谁能告诉我怎么做?
我想出了这个只是挂起,我猜是因为它阻止子进程终止.
#!/usr/bin/python
from subprocess import Popen, PIPE, STDOUT
cmd = 'socat -d -d PTY: PTY: &'
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
output = p.stdout.read()
# Process the output
print(output)
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
编辑:似乎它可能写入stderr,但脚本仍然只是在有和没有从stderr读取甚至没有读取.
每次我执行我的Python脚本时,它似乎都挂在这一行:
lines = sys.stdin.readlines()
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决/避免这种情况?
编辑
这就是我正在做的事情lines:
lines = sys.stdin.readlines()
updates = [line.split() for line in lines]
Run Code Online (Sandbox Code Playgroud)
编辑2
我从git hook运行这个脚本,所以在EOF周围有吗?
我为我的应用程序编写了一个简单的python脚本,并预定了一些快速命令,如make等.
我编写了一个运行系统命令的函数(linux):
def runCommand(commandLine):
print('############## Running command: ' + commandLine)
p = subprocess.Popen(commandLine, shell = True, stdout = subprocess.PIPE)
print (p.stdout.read().decode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
一切都很好,除了一些事情:
我正在使用cmake,它的输出是彩色的.有没有机会在输出中保存颜色?
我可以在流程完成后查看输出.例如,make运行很长一段时间但我只能在完全编译后看到输出.如何异步进行?
我注意到两种不同的行为,两种方法应该产生相同的结果.
目标 - 使用子进程模块执行外部程序,发送一些数据并读取结果.
外部程序是PLINK,平台是WindowsXP,Python版本3.3.
主要想法 -
execution=["C:\\Pr..\\...\\plink.exe", "-l", username, "-pw", "***", IP]
a=subprocess.Popen(execution, bufsize=0, stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=False)
con=a.stdout.readline()
if (con.decode("utf-8").count("FATAL ERROR: Network error: Connection timed out")==0):
a.stdin.write(b"con rout 1\n")
print(a.stdout.readline().decode("utf-8"))
a.stdin.write(b"infodf\n")
print(a.stdout.readline().decode("utf-8"))
else:
print("ERROR")
a.kill()
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.
现在,我想能够做一个循环(在每次写入子进程的stdin之后),等待直到子进程的stdout的EOF,打印它,然后是另一个stdin命令,依此类推.
所以我首先尝试了之前关于相同主题的讨论产生(从subprocess命令实时输出,逐行读取子进程stdout,python,subprocess:从子进程读取输出).
并且它没有工作(它永远挂起),因为PLINK进程保持活着,直到我自己杀死它,因此没有使用等待子进程的stdout到达EOF或在stdout为真时进行循环,因为它在我杀了它之前一直都是真的.
所以我每次写stdin时都决定从stdout读两次(对我来说很好) -
execution=["C:\\Pr..\\...\\plink.exe", "-l", username, "-pw", "***", IP]
a=subprocess.Popen(execution, bufsize=0, stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=False)
con=a.stdout.readline()
if (con.decode("utf-8").count("FATAL ERROR: Network error: Connection timed out")==0):
a.stdin.write(b"con rout 1\n")
print(a.stdout.readline().decode("utf-8"))
print(a.stdout.readline().decode("utf-8")) //the extra line [1]
a.stdin.write(b"infodf\n") …Run Code Online (Sandbox Code Playgroud) 我正在使用subprocess.popen运行子程序.当我从命令窗口(cmd.exe)启动我的Python程序时,程序会随着程序的发展在窗口中写入一些信息和日期.
当我不在命令窗口中运行我的Python代码时,它会为这个子程序的输出打开一个新的命令窗口,我想避免这种情况.当我使用以下代码时,它不会显示cmd窗口,但它也不会打印状态:
p = subprocess.Popen("c:/flow/flow.exe", shell=True, stdout=subprocess.PIPE)
print p.stdout.read()
Run Code Online (Sandbox Code Playgroud)
如何在程序输出中显示子程序的输出?
python ×9
subprocess ×5
stdout ×2
asynchronous ×1
buffered ×1
cmd ×1
flush ×1
hang ×1
ipython ×1
jupyter ×1
printing ×1
python-3.3 ×1
python-3.x ×1
readlines ×1
stdin ×1
windows ×1