0

I am trying to get the output of a shell command I try to execute using python but I get an error.

How can I get the response/return value from executing a bash command

This is what I have done:

import subprocess
import time

# NAMESPACE = input("Namespace: ")

# # Create a namespace
# subprocess.call(["kubectl", "create", "namespace", NAMESPACE])

# build a docker image to deploy the application
DOCKER_OUTPUT = subprocess.call(["docker", "build", "-t", "banuka/node-web-app", "."])
print("Docker output is: " + DOCKER_OUTPUT)

Somehow this gives an error:

unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/jananath/Desktop/python-script/Dockerfile: no such file or directory Traceback (most recent call last): File "/home/jananath/Desktop/python-script/bitesize-platform-troubleshooter/test/test.py", line 11, in print("Docker output is: " + DOCKER_OUTPUT) TypeError: can only concatenate str (not "int") to str

Can someone please help me to print the response without getting this error (from python)?

  • Is the dockerfile in the same directory as your python script? – alex067 Nov 26 '19 at 19:00
  • Yes. command execute without any error when I run the command in a normal shell in the same directory where the `Dockerfile` is and yes, `python` file also in the same directory. – BhagyaKolitha Jayalath Nov 26 '19 at 19:04
  • Possible duplicate of https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output – manesioz Nov 26 '19 at 19:19

3 Answers3

1

The result of system commands is usually an integer associated with an exit status. You can do print("Docker output is: " + str(DOCKER_OUTPUT)") To convert the int to a String, or you can use other Python string formatting options (depending on your Python version).

Example: f-string print(f"Docker output is {DOCKER_OUTPUT}")

Example: .format() print("Docker output is {}".format(DOCKER_OUTPUT))

c_sagan
  • 482
  • 3
  • 15
0

If you want a more verbose output (i.e. not just the zero/nonzero exit status) you can do the following:

cmd = 'docker build -t banuka/node-web-app .'
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output, err = p.communicate()
print('This is the output: {}, and this is the error: {}'.format(output, error))
manesioz
  • 798
  • 1
  • 9
  • 21
0

You shouldn't use subprocess.call it's in the old deprecated API if you lookup the docs you can find this example.

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')

The correct way of capturing the out put would be for example:

out=subprocess.run(["ls", "-l","/foo/bar"], capture_output=True).stdout

Make sure you are running the latest version of python you are able to and the latest set of functions because they tend to be more convenient and easier to use.

ascoder
  • 595
  • 3
  • 14