-1

I have been trying for hours to get the output of a shell command as a string. I have tried both subprocess and os, neither of which have worked, and within subprocess I have tried check_output(), getoutput(), Popen(), communicate(), and everything else I've been able to find on this site and many others.

Sometimes I've had errors such as FileNotFoundError: [WinError 2] The system cannot find the file specified though I have been able to fix these relatively swiftly, however when the code does actually work, and I try to print the output of the command, either it returns nothing (as in, it prints blank space), or it prints (b'', b'') or (b'', None).

decode() doesn't work, encoding doesn't change anything and I even tried:

subpr = str(process)

which, of course, did nothing.

How do you get the output of a shell command, as a string?

Other attempts:

subpr = (Popen(commandRun,shell=True,stdout=PIPE,stderr=PIPE,universal_newlines=True).communicate()[0])

process = subprocess.getoutput(commandRun)

process = subprocess.check_output(commandRun,shell=True)

process = subprocess.check_output(commandRun,stdout=PIPE,shell=True)

process = Popen(commandRun,stdout=PIPE,shell=True)
subpr = process.communicate()[0]

output = Popen(commandRun,shell=True,stdout=PIPE,stderr=PIPE)
subpr = output.communicate()

Imported:

import subprocess
from subprocess import Popen, PIPE

There is not much more code to add. I haven't written anything regarding subprocess other than that one broken line.

vb4
  • 1
  • 2
  • 2
    Would you please add some more code? There is probably something wrong in your invocation of `subprocess`. – DaLynX Dec 20 '21 at 11:30
  • This line `subpr = (subprocess.Popen(commandRun,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True).communicate()[0])` works too. Are you sure there is nothing wrong with the process you are running? – DaLynX Dec 20 '21 at 11:40
  • Which python version are you using? – SAIJAL Dec 20 '21 at 11:41
  • I don't know, it's possible. The `commandRun` variable is a combination of two commands, and I may have strung them together incorrectly. The first part is setting the file path to a raw path on my disk, then the second part is the actual command. The command doesn't work without the file path. – vb4 Dec 20 '21 at 11:43
  • Are you using Windows? – dan Dec 20 '21 at 11:44
  • @SAIJAL Python 3.9.5 – vb4 Dec 20 '21 at 11:44
  • @dan Yes, Windows 10 – vb4 Dec 20 '21 at 11:45
  • @MauriceMeyer "1" – vb4 Dec 20 '21 at 11:46
  • The return code indicates that the command failed. If you are trying to run Unix commands on Windows, that won't work out of the box; install the required tools from a third party such as Cygwin, or run the code on a Unix-like platform instead (you will be happy to leave Windows behind for many other reasons too anyway). – tripleee Dec 20 '21 at 11:57
  • Also, you can't combine two commands in one subprocess unless you specify `shell=True` (in which case `commandRun` should be a string, not a list). – tripleee Dec 20 '21 at 11:58
  • @tripleee You are right, it's an issue with the command, though I have tried `shell=True` and `commandRun` has been a string from the start – vb4 Dec 20 '21 at 12:00
  • As long as you steadfastly refuse to show us the actual commands, we can only endlessly speculate. If the duplicate doesn't solve your problem, probably look at related questions; if you still can't find one which covers your specific problem, please [edit] to provide an actual [mre] and we can discuss reopening. – tripleee Dec 20 '21 at 12:02
  • Check out this duplicate: https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output and word search it for `Windows` also. – dan Dec 20 '21 at 12:02

1 Answers1

0

How are you trying to use these?

I have the following code that works, redirecting STDERR to STDOUT, because I wanted to have them merged:

import subprocess
args = ["whoami"]
run = subprocess.run(args, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(run.stdout)

If you want to pipe processes together, the best way is probably to put the popes in arguments of Popen, see https://docs.python.org/3/library/subprocess.html#replacing-bin-sh-shell-command-substitution

p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
DaLynX
  • 328
  • 1
  • 11
  • This prints nothing for me. I'm trying to use it to run a command in shell, then take the output of said command and write it to a file (while also printing it). – vb4 Dec 20 '21 at 11:39
  • This prints nothing with `whoami`? Or with another command? – DaLynX Dec 20 '21 at 11:41
  • `whoami` works, so I think you are correct that it is something to do with my command – vb4 Dec 20 '21 at 11:48
  • Are you using the correct separators for the command? | pipes (pipelines) the standard output (stdout) of one command into the standard input of another one. Note that stderr still goes into its default destination, whatever that happens to be. && executes the right-hand command of && only if the previous one succeeded. – SAIJAL Dec 20 '21 at 11:48
  • @SAIJAL I think it is likely something to do with the file path - the first half of the command is setting the path of the command – vb4 Dec 20 '21 at 11:50
  • What do you mean by "setting the path"? If you need a specific working directory you can pass it as `cwd=...` – DaLynX Dec 20 '21 at 11:53
  • Running `grep` in a subprocess is inelegant and wasteful, though; Python can perfectly well extract the lines which contain a particular string or match a particular regex. – tripleee Dec 20 '21 at 11:54
  • This is an example from the python docs... It's not production code... – DaLynX Dec 20 '21 at 11:54