Ho un caso di voler eseguire il seguente comando shell in Python e ottenere l'output,Python - come eseguire i comandi della shell con pipe?
echo This_is_a_testing | grep -c test
ho potuto utilizzare questo codice python per eseguire il comando shell sopra pitone,
>>> import subprocess
>>> subprocess.check_output("echo This_is_a_testing | grep -c test", shell=True)
'1\n'
Tuttavia, come io non voglio usare il "= shell true" opzione, ho provato il seguente codice python,
>>> import subprocess
>>> p1 = subprocess.Popen(["echo", "This_is_a_testing"], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout)
>>> p1.stdout.close()
>>> p2.communicate()
(None, None)
mi chiedo perché l'uscita è "Nessuno" come Ho fatto riferimento alle descrizioni nella pagina Web: http://docs.python.org/library/subprocess.html#subprocess.PIPE
Avevo perso alcuni punti nel mio codice? Qualche suggerimento/idea? Grazie in anticipo.
aggiornato la risposta – avasal
correlati: [Come si usa subprocess.Popen per collegare più processi da tubi?] (http://stackoverflow.com/q/295459/4279) – jfs