有没有办法在Python 2.x中将二进制输出写入sys.stdout?在Python 3.x中,您可以使用sys.stdout.buffer(或分离stdout等等),但我无法找到任何Python 2.5/2.6的解决方案.
编辑,解决方案:来自ChristopheD的链接,如下:
import sys
if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
Run Code Online (Sandbox Code Playgroud)
编辑:我正在尝试将PDF文件(二进制形式)推送到stdout以便在Web服务器上提供服务.当我尝试使用sys.stdout.write编写文件时,它会将各种回车符添加到二进制流中,导致PDF呈现损坏.
编辑2:对于这个项目,遗憾的是我需要在Windows Server上运行,因此Linux解决方案已经完成.
Simply Dummy示例(从磁盘上的文件读取,而不是动态生成,只是因为我们知道生成代码不是问题):
file = open('C:\\test.pdf','rb')
pdfFile = file.read()
sys.stdout.write(pdfFile)
Run Code Online (Sandbox Code Playgroud) 要将stdout和stderr重定向(并附加)到文件中,同时还在终端上显示它,我这样做:
command 2>&1 | tee -a file.txt
Run Code Online (Sandbox Code Playgroud)
但是,有没有其他方法可以做到这一点,以便我获得退出状态的准确值?
也就是说,如果我测试$?,我想看到退出状态command,而不是退出状态tee.
我知道我可以${PIPESTATUS[0]}在这里使用而不是$?,但我正在寻找另一种不需要检查的解决方案PIPESTATUS.
我试图使用以下代码临时将System.out重定向到/ dev/null但它不起作用.
System.out.println("this should go to stdout");
PrintStream original = System.out;
System.setOut(new PrintStream(new FileOutputStream("/dev/null")));
System.out.println("this should go to /dev/null");
System.setOut(original);
System.out.println("this should go to stdout"); // This is not getting printed!!!
Run Code Online (Sandbox Code Playgroud)
有人有主意吗?
是否可以在批处理文件中使用管道stdin流?
我希望能够将一个命令的输出重定向到我的批处理文件process.bat列表中,这样:
C:\>someOtherProgram.exe | process.bat
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试看起来像:
echo OFF
setlocal
:again
set /p inputLine=""
echo.%inputLine%
if not (%inputLine%)==() goto again
endlocal
:End
Run Code Online (Sandbox Code Playgroud)
当我用type testFile.txt | process.bat它测试时,它会反复打印出第一行.
还有另外一种方法吗?
我想将python脚本的子进程'stdout和stdin指向同一个文件.我不知道的是如何使两个来源的线条区分开来?(例如,带有感叹号的stderr行前缀.)
在我的特定情况下,不需要对子进程进行实时监视,执行的Python脚本可以等待其执行结束.
为了检测控制台,正确sys.stderr还是sys.stdout,我正在做以下测试:
if hasattr(sys.stderr, "isatty") and sys.stderr.isatty():
if platform.system()=='Windows':
# win code (ANSI not supported but there are alternatives)
else:
# use ANSI escapes
else:
# no colors, usually this is when you redirect the output to a file
Run Code Online (Sandbox Code Playgroud)
现在,通过IDE(如PyCharm)运行此Python代码时,问题变得更加复杂.最近PyCharm添加了对ANSI的支持,但第一次测试失败:它具有isatty属性但设置为False.
我想修改逻辑,以便正确检测输出是否支持ANSI着色.一个要求是在任何情况下我都不应该在输出重定向到文件时输出一些东西(对于控制台,它是可以接受的).
通过https://gist.github.com/1316877添加了更复杂的ANSI测试脚本
我正在使用命令行实用程序,该实用程序需要传递文件名以将输出写入,例如
foo -o output.txt
Run Code Online (Sandbox Code Playgroud)
它写的唯一内容stdout是一条消息,表明它成功运行.我希望能够将写入的所有内容传递output.txt给另一个命令行实用程序.我的动机是output.txt的最终会被一个40 GB的文件,我不需要保留的,我宁愿管不是以逐步的方式对大量文件的工作流.
在这种情况下,有没有办法将实际输出(即output.txt)传递给另一个命令?我可以以某种方式神奇地传递stdout文件参数吗?
我有一个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) 在将程序作为systemd服务运行时,我无法将STDOUT和STDERR传送到文件.我已经尝试将以下内容添加到.service文件中:
ExecStart=/apppath/appname > /filepath/filename 2>&1
Run Code Online (Sandbox Code Playgroud)
但这不起作用.输出结果在/ var/log/messages中,可以使用journalctl查看,但我想要一个单独的文件.
我也试过设置,StdOutput=tty但找不到将其重定向到文件的方法.
任何帮助,将不胜感激.