I have problem with Popen.communicate().
I have script which return string.
Then I have wrote second script which takes that variable.
v = "./myscript arg1 arg2"
com = subprocess.Popen(v, shell=True).communicate()
print com
com returns (None, None). The point is that I can print inside first script the results, shell print result as well. I can't just assign that print to variable.
Of course first script returns value, not print it.
. communicate() writes input (there is no input in this case so it just closes subprocess' stdin to indicate to the subprocess that there is no more input), reads all output, and waits for the subprocess to exit.
Returned value If successful, popen() returns a pointer to an open stream that can be used to read or write to a pipe. If unsuccessful, popen() returns a NULL pointer and sets errno to one of the following values: Error Code. Description.
Popen Function The function should return a pointer to a stream that may be used to read from or write to the pipe while also creating a pipe between the calling application and the executed command. Immediately after starting, the Popen function returns data, and it does not wait for the subprocess to finish.
Python method popen() opens 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 in open() function.
From the docs:
Note that if you want to send data to the process’s stdin, you need to create the
Popen
object withstdin=PIPE
. Similarly, to get anything other thanNone
in the result tuple, you need to givestdout=PIPE
and/orstderr=PIPE
too.
Hence, create the Popen
object with:
subprocess.Popen("./myscript arg1 arg2", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With