2011-02-13 15 views
14

Ben outf/errf Yok veya (stdout/stderr için farklı dosyalar) bir dosya tanımlayıcı ya olduğundaPython alt işlemi, çocukların çıktılarını dosyaya ve terminale mi veriyor?

subprocess.call(cmdArgs,stdout=outf, stderr=errf) 

kullanarak yürütülebilir bir dizi yürütür bir komut dosyası çalıştırıyorum.

Her bir exe'yi çalıştırabilmem için, stdout ve stderr dosya ve terminallere birlikte yazılacak mı?

+1

[asyncio sürümü] (http://stackoverflow.com/a/25960956/4279) – jfs

cevap

22

call() işlevi yalnızca Popen(*args, **kwargs).wait()'dur. Sen Popen doğrudan arayıp p.stdout okuma stdout=PIPE argüman kullanabilirsiniz:

import sys 
from subprocess import Popen, PIPE 
from threading import Thread 

def tee(infile, *files): 
    """Print `infile` to `files` in a separate thread.""" 
    def fanout(infile, *files): 
     for line in iter(infile.readline, ''): 
      for f in files: 
       f.write(line) 
     infile.close() 
    t = Thread(target=fanout, args=(infile,)+files) 
    t.daemon = True 
    t.start() 
    return t 

def teed_call(cmd_args, **kwargs):  
    stdout, stderr = [kwargs.pop(s, None) for s in 'stdout', 'stderr'] 
    p = Popen(cmd_args, 
       stdout=PIPE if stdout is not None else None, 
       stderr=PIPE if stderr is not None else None, 
       **kwargs) 
    threads = [] 
    if stdout is not None: threads.append(tee(p.stdout, stdout, sys.stdout)) 
    if stderr is not None: threads.append(tee(p.stderr, stderr, sys.stderr)) 
    for t in threads: t.join() # wait for IO completion 
    return p.wait() 

outf, errf = open('out.txt', 'w'), open('err.txt', 'w') 
assert not teed_call(["cat", __file__], stdout=None, stderr=errf) 
assert not teed_call(["echo", "abc"], stdout=outf, stderr=errf, bufsize=0) 
assert teed_call(["gcc", "a b"], close_fds=True, stdout=outf, stderr=errf) 
+0

Teşekkürler, altprocess.Popen yerine birden çok exec çalıştırmak istiyorum. Her çalıştırıcının farklı bir dosyaya ve – user515766

+1

@ user515766 terminaline yazdığı yer): çözüm aynıdır: 'stdout', 'stderr' 'PIPE 'olarak ayarlayın ve' tee() 'yi çağırmak istediğinizde Birden fazla yere yaz. – jfs

+0

Hızlı yanıt için teşekkürler, ama işe yaramıyor. dış işlem yalnızca OS düzeyindeki dosya tanıtıcılarını görür (dosya nesnelerinizde numaralı numaradan fileno() yönteminden alırsınız). http://bytes.com/topic/python/answers/541085-extend-file-type adresine bakın. – user515766

0

Kullanım | tee terminalde çıkış elde ederken out.txt adlı bir dosyaya çıktı yönlendirmek için.

import subprocess 

# Run command and redirect it by | tee to a file named out.txt 
p = subprocess.Popen([command, '|', 'tee', 'out.txt']) 
p.wait() 

Windows platformunda, | tee. Powershell'i kullanmamız gerek. Yani üçüncü satırında komut haline gelir: Bu arada

# Run command in powershell and redirect it by | tee to a file named out.txt 
p = subprocess.Popen(['powershell','command, '|', 'tee', 'out.txt']) 

, stdout'u basılır ve stdout'u ayrıca dosya out.txt saklanacaktır.

İlgili konular