python - Resource unavailable when piping subprocess -
i trying find path matlab executable using python when not in path. using subprocess.popen execute locate , grepping result, creates resource unavailable error:
locate = subprocess.popen(['locate', 'matlab'], stdout=subprocess.pipe) grep = subprocess.popen(['grep', '/bin/matlab$'], stdin=locate.stdout, stdout=subprocess.pipe, stderr=subprocess.pipe) result, err = grep.communicate() matlab_path = result.decode('utf-8').split() the result variable empty , err variable :
b'grep: (standard input): resource temporarily unavailable\n'
i have tried code on linux python 3.5.2 , 3.6.1 , work:
locate = subprocess.popen(['locate', 'find'], stdout=subprocess.pipe) grep = subprocess.popen(['grep', '/bin/find$'], stdin=locate.stdout, stdout=subprocess.pipe, stderr=subprocess.pipe) grep.communicate() (b'/usr/bin/find\n', b'') for records: locate find gives 1619 lines. completeness have tried locate fdafad (gibberish) , works.
it work when code in script.
edit:
try use communicate interact between 2 processess:
locate = subprocess.popen(['locate', 'find'], stdout=subprocess.pipe) stdout, stderr = locate.communicate() grep = subprocess.popen(['grep', '/bin/find$'], stdin=subprocess.pipe, stdout=subprocess.pipe, stderr=subprocess.pipe) print(grep.communicate(input=stdout)) note: second part of answer has been written before asker updated question information path
however there better ways find executables using python:
from distutils.spawn import find_executable find_executable('find') '/usr/bin/find' if insist in using shell functions, why don't use which.
Comments
Post a Comment