0

This question is the same as python subprocess.Popen - write to stderr. But there is no correct answers.

I know this is correct.

import sys
sys.stderr.write('abcd')

Here is the code of subprocess(named input). And I want the child process (input) to print the string written by parent process.

int main(int argc, char* argv[], char* envp[]){
    char buf[5];
    read(2, buf, 4);
    buf[4]=0;
    printf("%s\n", buf);
}

I want to write to a Python subprocess' stderr. However, this is not correct. (Maybe you can correct this or just introduce me another way.)

from subprocess import *
p = Popen(['./input'],stderr=PIPE)
p.stderr.write('abcd')

I got error like this.

IOError: File not open for writing

Thanks for your help.

Community
  • 1
  • 1
pinohans
  • 3
  • 4
  • 1
    If you succeed in doing this, where do you expect "abcd" to appear? It will not appear on the terminal, because one end of the pipe is connected to the subprocess and the other to the parent process. In theory it could appear in the subprocess, but it will only be *writing* to stderr, not *reading* from it. What do you want to happen? – Arthur Tacca Apr 03 '17 at 16:01
  • 1
    The subprocess writes to its stderr. By making it a pipe, you are setting up the parent to read from it. If you want the subprocess to write to its stderr, the process it execs must do so. If you want that error message to go to the stderr of the parent process, then you should do nothing, and the child will inherit the parent's stderr as its own. – William Pursell Apr 03 '17 at 16:02
  • stderr is generally opened as a read-only stream when you open a subprocess like that. Of course you are getting that error. What are you actually trying to do and why. Some more detailed and specific background might really help in this case. – Mad Physicist Apr 03 '17 at 16:29
  • Sorry, I have added the code of subprocess just now. Thanks a lot @ArthurTacca and William. I just want the child process (input) to print the string written by parent process. – pinohans Apr 03 '17 at 16:37
  • @MadPhysicist. Thanks a lot. You are right, and I have updated the question. – pinohans Apr 03 '17 at 16:41
  • The process writes to its stderr, not you. You write to the process stdin. – wim Apr 03 '17 at 16:45

1 Answers1

2

os.pipe()

Open a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as the corresponding argument to the built-in open() function. The exit status of the command (encoded in the format specified for wait()) is available as the return value of the close() method of the file object, except that when the exit status is zero (termination without errors), None is returned.

Change your Python code to this:

from subprocess import *
import os

r, w = os.pipe()
p = Popen(['./input'],stderr=r, stdout=PIPE)
os.close(r)
os.write(w, b'abcd')
os.close(w)
print(repr(p.communicate()[0]))

You can read Python os.pipe Examples.

RaminNietzsche
  • 2,683
  • 1
  • 20
  • 34