Python: inferring output from subprocess.call using stdout

So, I'm trying to save the output from my subprocess.call , but I keep getting the following error: AttributeError: 'int' object has no attribute 'communicate'

The code is as follows:

 p2 = subprocess.call(['./test.out', 'new_file.mfj', 'delete1.out'], stdout = PIPE) output = p2.communicate[0] 
+4
source share
3 answers

You are looking for subprocess.Popen() instead of call() .

You also need to change it to p2.communicate()[0] .

+4
source

This is because subprocess.call returns int:

 subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False) Run the command described by args. Wait for command to complete, then return the returncode attribute. 

It looks like you want subprocess.Popen ().

Here is a typical piece of code that I should do:

 p = Popen(cmd, stdout=PIPE, stderr=PIPE, bufsize=256*1024*1024) output, errors = p.communicate() if p.returncode: raise Exception(errors) else: # Print stdout from cmd call print output 
+3
source

You must use a subprocess

  try: subprocess.check_output(['./test.out', 'new_file.mfj', 'delete1.out'], shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as exception: print exception.output 
0
source

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


All Articles