23 Mart 2010, Salı
Alt işlemi deposu çıktı.Popen bir dize çağrısı
Bir sistem çağrısı Python ve Python programında kullanabileceğim bir dizeye çıktı deposu yapmaya çalışıyorum.
#!/usr/bin/python
import subprocess
p2 = subprocess.Popen("ntpq -p")
Bu öneriler de dahil olmak üzere bir kaç şey denedim:
Retrieving the output of subprocess.call()
ama olmadan herhangi bir şans.
CEVAP
23 Mart 2010, Salı
Python 2.7 veya Python 3'te
Doğrudan Popen
bir nesne yapmak yerine, subprocess.check_output()
function string içinde bir komut çıktısını depolamak için kullanabilirsiniz:
from subprocess import check_output
out = check_output(["ntpq", "-p"])
2.4-2.6 Python
communicate
yöntemi kullanın.
import subprocess
p = subprocess.Popen(["ntpq", "-p"], stdout=subprocess.PIPE)
out, err = p.communicate()
out
istediğiniz nedir.
Yukarıda hakkında önemli not cevaplar
Komutu ben nasıl geçtiğini unutmayın. "ntpq -p"
Bu örnek, başka bir sorun ortaya getiriyor. Popen
kabuk involke olmadığından, komut listesi ve seçenekleri["ntpq", "-p"]
kullanırsınız.
Bunu Paylaş:
Dize çıktı: format veya herhangi bir p...
Nasıl ISO 8601 biçimlendirilmiş bir di...
Python alt işlemi.Popen "OSError:...
Nasıl bir dize ortasından kültür duyar...
Nasıl 'gerçek' bir alt işlem...