Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess.check_output() module object has out attribute 'check_output'

Tags:

python

hadoop

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?

like image 792
Liondancer Avatar asked Nov 12 '14 18:11

Liondancer


1 Answers

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

like image 186
Padraic Cunningham Avatar answered Oct 03 '22 04:10

Padraic Cunningham