Goutham HM
Goutham HM

Reputation: 21

Python subprocess.popen returns empty string

import subprocess
cd=['sudo','./interface','-a','</tmp/vol.js']
p = subprocess.Popen(cd, stdout = subprocess.PIPE,stderr=subprocess.PIPE, stdin=subprocess.PIPE)

Above code returns null but when I run same same command i.e sudo ./interface -a </tmp/vol.js works totally fine

Upvotes: 1

Views: 3350

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1124928

You are using shell-specific redirection syntax (</tmp/vol.js) but subprocess.Popen() doesn't run your command in a shell.

You'd need to either run the command in a shell (pass in a string, not a list, and set shell=True), or open vol.js yourself and write that to the process through the stdin pipe you opened:

import subprocess
command = ('sudo', './interface', '-a')
with open('/tmp/vol.js') as input_stream:
    p = subprocess.Popen(command, stdout=subprocess.PIPE, 
                         stderr=subprocess.PIPE, stdin=input_stream)
    stdout, stderr = p.communicate()

Note that I passed in the open vol.js file object as the input stream here.

Upvotes: 1

soumen
soumen

Reputation: 66

Can you try this.

import subprocess
cd = ['sudo','./interface','-a','</tmp/vol.js']
cd_cmd = " ".join(cd)
o, e = subprocess.Popen(cd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

The output is stored in variable "o" and stderr in "e"

Upvotes: 1

Related Questions