Ich muss über die Ausgabe eines Befehls Schleife. Ich dachte, ich würde subprocess.check_output
verwenden, jetzt habe ich zwei Probleme.Python Schleife über subprocess.check_output von Linie
Hier ist eine Datei:
foo
bar
Hier ist mein Python-Skript:
import subprocess
for line in subprocess.check_output(['cat', 'foo']):
print "%r" % line
Und hier ist, was ich bekommen:
$ python subp.py
'f'
'o'
'o'
'\n'
'b'
'a'
'r'
'\n'
erwarte ich:
$ python subp.py
'foo\n'
'bar\n'
könnte auch [ '.splitlines()'] (https://docs.python.org /library/stdtypes.html#str.splitlines) oder 'p =' ['subprocess.Popen'] (https://docs.python.org/library/subprocess.html#popen-constructor)' (..., stdout = subprocess.PIPE); für Zeile in p.stdout: ... ' – bufh