6

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

Code is as follows:

p2 = subprocess.call(['./test.out', 'new_file.mfj', 'delete1.out'], stdout = PIPE) output = p2.communicate[0] 
1
  • The answers which recommend Popen were basically correct in 2012 when this question was asked, but the modern correct answer is to use subprocess.run, or subprocess.check_output if you need a simple API and/or compatibility back to older Python versions. The subprocess documentation spells this out, in the very first paragraph of the first section; "The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle." Commented Mar 4, 2022 at 12:32

3 Answers 3

5

You're looking for subprocess.Popen() instead of call().

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

Sign up to request clarification or add additional context in comments.

1 Comment

This was correct in 2012, but the recommended solution now is to avoid Popen unless your use case requires it.
4

That's because subprocess.call returns an 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's a typical piece of code I have to do this:

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 

Comments

1

You should use 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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.