使用subprocess.call保留stdout和stderr顺序

时间:2015-04-23 09:02:50

标签: python subprocess cross-platform stdout stderr

有没有办法在使用subprocess模块时保留stdout和stderr顺序?

的hello.c

        int number = items.Count;
        int number2 = number / 2;

        List<TableItem> firstList = items.GetRange(0, number2);
        List<TableItem> secondList = items.GetRange(number2, number % 2 == 0? number2 : number2 + 1);


        listView1.Adapter = new HomeScreenAdapter(this, firstList);
        listView2.Adapter = new HomeScreenAdapter (this, secondList);

test.py

#include <stdio.h>
void main()
{
   printf("o1 ");
   fprintf(stderr, "e1 ");
   printf("o2 ");
   fprintf(stderr, "e2 ");
   printf("o3 ");
   fprintf(stderr, "e3 ");
}

输出:

import subprocess

print ("Test 1")
sp = subprocess.Popen("hello", stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
output = str(sp.communicate()[0])
print(output)

print ("Test 2")
sp = subprocess.Popen("hello", stdout = subprocess.PIPE, stderr = subprocess.PIPE)
stdout, stderr = sp.communicate() 
print(stdout + stderr)

print ("Test 3")
print(subprocess.check_output("hello", stderr=subprocess.STDOUT))

0 个答案:

没有答案