The code you are running would print a human-readable representation of a CompletedProcess object which includes, but contains much more than, the actual output from your subprocess, on a single line.
Python 3.7.2 (default, Mar 25 2020, 10:15:53)
[Clang 11.0.3 (clang-1103.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> x = subprocess.run(['printf', '%s\n', 'foo', 'bar', 'baz'], capture_output=True)
>>> print(x)
CompletedProcess(args=['printf', '%s\n', 'foo', 'bar', 'baz'], returncode=0, stdout=b'foo\nbar\nbaz\n', stderr=b'')
To actually print only the output, try
>>> print(x.stdout.decode())
foo
bar
baz
Better yet, have Python decode it for you.
import subprocess
def executable_shell():
# to run cosmo file
x = subprocess.run(
# Just the command, as a list of arguments, so we can avoid shell=True
['./COSMO'],
# Specify which directory to run it in
cwd='../build',
# Throw an exception if the command fails (optional but recommended)
check=True,
# Have Python decode the output as text instead of raw bytes
text=True,
# Capture output, as before
capture_output=True)
return x.stdout
print(executable_shell())
Notice how I added text=True (and also refactored to use check=True and get rid of shell=True) and how the function now returns the result, and the caller prints it (or does something else with it, as the case may be). Generally, you want functions to return results rather than print them; this makes it easier to reuse them for other tasks.
text=Truein options, and then accessx.stdoutto get the stream of bytes in return.xis not the output, it is an object that encapsulate the process management.print("{}\r".format(x)). Keep in mind thatsubprocess.run()returns a CompletedProcess instance aka a process that has finished whencapture_outputistruestdout and stderr will be captured