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)?

3
  • Is the dockerfile in the same directory as your python script? Commented Nov 26, 2019 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. Commented Nov 26, 2019 at 19:04
  • Possible duplicate of stackoverflow.com/questions/4760215/… Commented Nov 26, 2019 at 19:19

3 Answers 3

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))

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

Comments

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))

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.