stdout

Piping output of subprocess.Popen to files

雨燕双飞 提交于 2019-12-17 10:33:36
问题 I need to launch a number of long-running processes with subprocess.Popen , and would like to have the stdout and stderr from each automatically piped to separate log files. Each process will run simultaneously for several minutes, and I want two log files ( stdout and stderr ) per process to be written to as the processes run. Do I need to continually call p.communicate() on each process in a loop in order to update each log file, or is there some way to invoke the original Popen command so

Redirecting FORTRAN (called via F2PY) output in Python

北城以北 提交于 2019-12-17 10:29:23
问题 I'm trying to figure out how to redirect output from some FORTRAN code for which I've generated a Python interface by using F2PY. I've tried: from fortran_code import fortran_function stdout_holder = sys.stdout stderr_holder = sys.stderr sys.stdout = file("/dev/null","w") fortran_function() sys.stdout.close() sys.stderr.close() sys.stdout = stdout_holder sys.stderr = stderr_holder This is the de facto method of redirecting output in Python, but it doesn't seem to work in this case (i.e., the

Disable buffering on redirected stdout Pipe (Win32 API, C++)

帅比萌擦擦* 提交于 2019-12-17 09:49:16
问题 I'm spawning a process from Win32 using CreateProcess , setting the hStdOutput and hStdError properties of STARTUPINFO to pipe handles created with CreatePipe . I've got two threads reading the pipes, waiting for data to become available (or the process to complete, at which point it checks that there is no data left before terminating the thread). As data becomes available, I write the output out to effectively a big textbox. What's happening is the output is being buffered, so a slow

What is the simplest way to write to stdout in binary mode?

≯℡__Kan透↙ 提交于 2019-12-17 07:47:06
问题 I've been trying to figure out the best way to write binary data to stdout from a C program. It works fine on Linux, but I'm having issues when I compile on Windows because "\n" gets converted to "\r\n". Is there a standard way to write to stdout in some sort of binary mode which avoids newline conversion? If not, what is the simplest way to get Windows to stop doing this? I'm using GCC and MinGW. 回答1: You can use setmode(fileno(stdout), O_BINARY) Wrap it in an ifdef if you want to keep it

Redirecting stdio from a command in os.system() in Python

…衆ロ難τιáo~ 提交于 2019-12-17 07:35:19
问题 Usually I can change stdout in Python by changing the value of sys.stdout . However, this only seems to affect print statements. So, is there any way I can suppress the output (to the console), of a program that is run via the os.system() command in Python? 回答1: You could consider running the program via subprocess.Popen , with subprocess.PIPE communication, and then shove that output where ever you would like, but as is, os.system just runs the command, and nothing else. from subprocess

Piping both stdout and stderr in bash?

久未见 提交于 2019-12-17 07:04:33
问题 It seems that newer versions of bash have the &> operator, which (if I understand correctly), redirects both stdout and stderr to a file ( &>> appends to the file instead, as Adrian clarified). What's the simplest way to achieve the same thing, but instead piping to another command? For example, in this line: cmd-doesnt-respect-difference-between-stdout-and-stderr | grep -i SomeError I'd like the grep to match on content both in stdout and stderr (effectively, have them combined into one

Interactive input/output using python

瘦欲@ 提交于 2019-12-17 05:54:09
问题 I have a program that interacts with the user (acts like a shell), and I want to run it using python subprocess module interactively. That means, I want the possibility to write to stdin and immediately get the output from stdout. I tried many solutions offered here, but none of them seems to work for my needs. The code I've written based on Running an interactive command from within python import Queue import threading import subprocess def enqueue_output(out, queue): for line in iter(out

Read stdin stream in a batch file

故事扮演 提交于 2019-12-17 04:28:58
问题 Is it possible to use a piped stdin stream inside a batch file? I want to be able to redirect the output of one command into my batch file process.bat list so: C:\>someOtherProgram.exe | process.bat My first attempt looked like: echo OFF setlocal :again set /p inputLine="" echo.%inputLine% if not (%inputLine%)==() goto again endlocal :End When I test it with type testFile.txt | process.bat it prints out the first line repeatedly. Is there another way? 回答1: set /p doesn't work with pipes, it

How to redirect the output of a PowerShell to a file during its execution

拟墨画扇 提交于 2019-12-17 04:11:23
问题 I have a PowerShell script for which I would like to redirect the output to a file. The problem is that I cannot change the way this script is called. So I cannot do: .\MyScript.ps1 > output.txt How do I redirect the output of a PowerShell script during its execution? 回答1: Maybe Start-Transcript would work for you. First stop it if it's already running, then start it, and stop it when done. $ErrorActionPreference="SilentlyContinue" Stop-Transcript | out-null $ErrorActionPreference = "Continue

Why does 'java -version' go to stderr?

你说的曾经没有我的故事 提交于 2019-12-17 04:06:15
问题 Is there any special reason for the results of java -version going to stderr ? For example, this command executed from Windows' prompt line: java -version > java_version.txt leaves the file java_version.txt empty. EDIT: The same happens with the help printed out after executing java.exe without any parameters. EDIT: Just out of a sheer curiosity I checked whether it has been always like that and it turned out it actually has. java -version goes to stderr in JDK 1.1.8 and also in JDK 1.2.2,