标签: stdout

从Python函数中抑制stdout/stderr打印

我有一个Python脚本,它使用我的雇主提供的一些闭箱Python函数(即我无法编辑这些函数).当我调用这些函数时,它们正在打印输出到我想要压制的linux终端.我尝试过重定向stdout/stderr;

orig_out = sys.stdout
sys.stdout = StringIO()
rogue_function()
sys.stdout = orig_out
Run Code Online (Sandbox Code Playgroud)

但这没有抓住输出.我认为我通过Python调用的函数(上面的rogue_function())实际上是编译C代码的包装器,它们实际上正在进行打印.

有没有人知道我可以通过函数(以及函数调用的任何子函数)对stdout/stderr的任何打印进行"深度捕获"?

更新:

我最终采用了下面选定答案中概述的方法并编写了一个上下文管理器来压制stdout和stderr:

# Define a context manager to suppress stdout and stderr.
class suppress_stdout_stderr(object):
    '''
    A context manager for doing a "deep suppression" of stdout and stderr in 
    Python, i.e. will suppress all print, even if the print originates in a 
    compiled C/Fortran sub-function.
       This will not suppress raised exceptions, since exceptions are printed
    to stderr just before a script exits, and after the context manager …
Run Code Online (Sandbox Code Playgroud)

python stdout

32
推荐指数
3
解决办法
2万
查看次数

如何将缓冲区内容写入stdout?

有没有机会将当前vim缓冲区的内容写入stdout?

我想使用vim编辑通过stdin传递的内容 - 无需临时文件来检索修改后的内容(在Linux/Unix上).

是否有可能插件/脚本 - 退出或保存时将缓冲区内容放入stdout?

vi vim shell stdout

31
推荐指数
4
解决办法
1万
查看次数

Gradle:如何从测试STDERR/STDOUT输出到控制台?

(Gradle 3.2.1)我运行了一些java测试,它们在Stderr/Stdout中记录输出.如果我开始,我可以看到输出

gradle test --info
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,第三方图书馆的大量不需要的输出也是如此.

文档建议使用logging.caputureStandardError / logging.caputureStandardError (loglevel),但似乎没有任何影响.

tasks.withType(Test) {
   logging.captureStandardOutput LogLevel.QUIET
   logging.captureStandardError LogLevel.QUIET
}
Run Code Online (Sandbox Code Playgroud)

然后如果运行gradle test,则不在控制台中输出STDERR/STDOUT.

如何从控制台中的测试类中获取输出?

testing stdout stderr gradle

31
推荐指数
2
解决办法
1万
查看次数

删除Python中的最后一个STDOUT行

我试图找出如何抑制在stdout上显示用户输入.

raw_input()后跟任何print语句保留用户键入的内容.getpass()不显示用户键入的内容,但它确实保留了"Password:"提示符.

为了解决这个问题,我想只删除最后一行(它也会从行尾删除换行符).

python stdout command-prompt

30
推荐指数
1
解决办法
2万
查看次数

如何在Python中将stdout和stderr重定向到logger

我有一个记录器,有一个RotatingFileHandler.我想重定向所有StdoutStderr该记录器.怎么办?

python logging stdout python-3.x

30
推荐指数
8
解决办法
3万
查看次数

如何在freopen("out.txt","a",stdout)之后将输出重定向回屏幕

#include <stdio.h>

int main() {
    printf("This goes to screen\n");
    freopen("out.txt", "a", stdout);
    printf("This goes to out.txt");
    freopen("/dev/stdout", "a", stdout);
    printf("This should go to screen too, but doesn't\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我调用freopen将stdout重定向到out.txt然后我在文件上打印一些东西,现在我想将它重定向回屏幕,但是freopen("/ dev/stdout","a",stdout); 不起作用.有没有办法使用ANSI C或POSIX系统调用?

c posix stdout

29
推荐指数
5
解决办法
4万
查看次数

29
推荐指数
2
解决办法
4万
查看次数

回声相同的fputs(STDout)?

echo等于fputs( STDOUT ),还是echo写入不同的流?我已经使用PHP一段时间了,但我不太清楚在较低级别实际发生了什么.

php stdout stream

29
推荐指数
1
解决办法
2万
查看次数

使用python进行交互式输入/输出

我有一个与用户交互的程序(就像一个shell),我想以交互方式使用python子进程模块运行它.这意味着,我希望有可能写入stdin并立即从stdout获取输出.我在这里尝试了许多解决方案,但它们似乎都不能满足我的需求.

我编写的代码基于在python中运行交互式命令

import Queue
import threading
import subprocess
def enqueue_output(out, queue):
    for line in iter(out.readline, b''):
        queue.put(line)
    out.close()

def getOutput(outQueue):
    outStr = ''
    try:
        while True: #Adds output from the Queue until it is empty
            outStr+=outQueue.get_nowait()

    except Queue.Empty:
        return outStr

p = subprocess.Popen("./a.out", stdin=subprocess.PIPE, stout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize = 1)
#p = subprocess.Popen("./a.out", stdin=subprocess.PIPE, stout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False, universal_newlines=True)

outQueue = Queue()
errQueue = Queue()

outThread = Thread(target=enqueue_output, args=(p.stdout, outQueue))
errThread = Thread(target=enqueue_output, args=(p.stderr, errQueue))

outThread.daemon = True
errThread.daemon = True …
Run Code Online (Sandbox Code Playgroud)

python stdin subprocess interactive stdout

29
推荐指数
3
解决办法
3万
查看次数

Python在保留顺序的同时分别从子进程stdout和stderr读取

我有一个python子进程,我正在尝试从中读取输出和错误流.目前我有它的工作,但我只能在读完stderr之后阅读stdout.这是它的样子:

process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_iterator = iter(process.stdout.readline, b"")
stderr_iterator = iter(process.stderr.readline, b"")

for line in stdout_iterator:
    # Do stuff with line
    print line

for line in stderr_iterator:
    # Do stuff with line
    print line
Run Code Online (Sandbox Code Playgroud)

如您所见,stderrfor循环在stdout循环完成之前无法启动.如何修改它以便能够以正确的顺序读取这两行?

澄清:我仍然需要能够判断一条线是否来自stdout或者stderr因为我的代码中它们的处理方式不同.

python subprocess stdout stderr

29
推荐指数
5
解决办法
2万
查看次数

标签 统计

stdout ×10

python ×5

stderr ×2

subprocess ×2

c ×1

c++ ×1

command-prompt ×1

cout ×1

gradle ×1

interactive ×1

logging ×1

php ×1

posix ×1

python-3.x ×1

shell ×1

stdin ×1

stream ×1

testing ×1

vi ×1

vim ×1