Ashgonline
Ashgonline

Reputation: 41

Print staments are lost when execute a python file from subprocess.call method

I am running a Python code where I am executing a command to run another python file by using subprocess.call() method. The file I execute inside the subprocess.call() has some print statements in it but those statements are not printed on the console when I run this code.
Is there a way I can get them printed on the console ? Everything works fine when I execute this file standalone and not through the subprocess.call(). If I redirect the output of this command to a file then it gets printed in the file, but could not find a way to see it on console at runtime. Any help would be appreciated

Upvotes: 0

Views: 351

Answers (2)

Kolay.Ne
Kolay.Ne

Reputation: 1398

You might want to use subprocess.PIPE:

p = subprocess.call(..., stdout=subprocess.PIPE)
output, _ = p.communicate()
print(output)

(Written in the browser, but should work)

Btw, it's hard to understand your problem and suggest a solution when you don't provide any code (neither which you run with subprocess nor which uses it). Please, add it to get more specific and/or relevant answers

Upvotes: 0

frost-nzcr4
frost-nzcr4

Reputation: 1620

subprocess call has default params: stdout=None, stderr=None which you need to configure or use capture_output=True, see following docs: https://docs.python.org/3/library/subprocess.html

Upvotes: 1

Related Questions