Popen.communicate () returns (None, None), even if the script prints the results

I have a problem with Popen.communicate ().

I have a script that returns a string.

Then I wrote a second script that takes this variable.

v = "./myscript arg1 arg2" com = subprocess.Popen(v, shell=True).communicate() print com 

com returns (None, None). The fact is that I can print inside the first script of the results, the result of printing the shell. I cannot just assign this print to a variable.

Of course, the script first returns the value, rather than printing it.

+5
source share
2 answers

From docs :

Note that if you want to send data to the stdin process, you need to create a Popen object using stdin=PIPE . Similarly, to get a result other than None in the court for the result, you also need to specify stdout=PIPE and / or stderr=PIPE .

Therefore, create a Popen object with:

  subprocess.Popen('lsr', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
+5
source

Ok, rchang find a solution. This might be useful for novice screenwriters like me.

The main script did not print the result, but returned. I thought it would be the other way around.

Thanks!

0
source

Source: https://habr.com/ru/post/1210911/


All Articles