SORU
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ş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • girleffect

    girleffect

    20 Mayıs 2008
  • TechnoBuffalo

    TechnoBuffal

    8 HAZİRAN 2007
  • whiteboy7thst

    whiteboy7ths

    1 Temmuz 2009