I am playing with the subprocess library. I can perform
v = subprocess.check_output("ls")
and it works fine. However, when I SSH into a machine and I want to perform a hadoop command,
v = subprocess.check_output("hadoop fs -ls /path/to/file* | grep -oE '/(.*)'")
I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'check_output'
Not quite sure why. I think it might be my lack of understanding of subprocess. How can i fix this error?
check_output
was introduced in python 2.7
so won't work for < 2.7
.
You can use Popen
with communicate
to get the output.
from subprocess import PIPE,Popen
proc = Popen(['ls', '-l'], stdout=PIPE)
print(proc.communicate()[0].split())
This part of the docs may be useful
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With