Python子进程打印截断的输出
我已经编写了一个Python脚本,它将执行一个shell命令并将输出打印到stdout shell命令本身连接到后端服务并打印输出(逐行) 问题:脚本只打印输出的第一行。预期输出为多行输出,如下所示:Python子进程打印截断的输出,python,subprocess,Python,Subprocess,我已经编写了一个Python脚本,它将执行一个shell命令并将输出打印到stdout shell命令本身连接到后端服务并打印输出(逐行) 问题:脚本只打印输出的第一行。预期输出为多行输出,如下所示: //+This is line one of output (/something/something) This is line 2 of output… This is line 3 of output … … … Completed in 946 miliseconds import o
//+This is line one of output (/something/something)
This is line 2 of output…
This is line 3 of output
…
…
…
Completed in 946 miliseconds
import os
import sys
something = sys.argv[1]
ofile = sys.argv[2]
command = "here is the command part 1" + something + "command part 2 >> " + ofile
os.system(command)
f = open(ofile, "r")
data = f.read()
f.close()
print data
我已尝试使用以下方法获取命令的输出:
//+This is line one of output (/something/something)
方法1:
import os
import sys
something = sys.argv[1]
command = "here is the command part 1" + something + "command part 2"
os.system(command)
import sys
import subprocess
something = sys.argv[1]
command = "here is the command part 1" + something + "command part 2"
print subprocess.check_output(command, shell=True)
import sys
import subprocess
something = sys.argv[1]
command = "here is the command part 1" + something + "command part 2"
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print output
命令的输出(仅打印第一行):
方法2:
import os
import sys
something = sys.argv[1]
command = "here is the command part 1" + something + "command part 2"
os.system(command)
import sys
import subprocess
something = sys.argv[1]
command = "here is the command part 1" + something + "command part 2"
print subprocess.check_output(command, shell=True)
import sys
import subprocess
something = sys.argv[1]
command = "here is the command part 1" + something + "command part 2"
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print output
命令的输出:
//+This is line one of output (/something/something)
方法3:
import os
import sys
something = sys.argv[1]
command = "here is the command part 1" + something + "command part 2"
os.system(command)
import sys
import subprocess
something = sys.argv[1]
command = "here is the command part 1" + something + "command part 2"
print subprocess.check_output(command, shell=True)
import sys
import subprocess
something = sys.argv[1]
command = "here is the command part 1" + something + "command part 2"
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print output
如何打印完整的输出
更新:根据下面评论中的建议,我将命令的输出从Python脚本中重定向到一个文件,如下所示:
//+This is line one of output (/something/something)
This is line 2 of output…
This is line 3 of output
…
…
…
Completed in 946 miliseconds
import os
import sys
something = sys.argv[1]
ofile = sys.argv[2]
command = "here is the command part 1" + something + "command part 2 >> " + ofile
os.system(command)
f = open(ofile, "r")
data = f.read()
f.close()
print data
它仍然只打印输出的第一行
但是,如果我将命令的输出从命令行重定向到输出文件,则会捕获完整的输出
谢谢。您可以先尝试使用另一个命令,看看该命令本身是否有问题。还将命令stdout/stderr重定向到python之外的文件,以查看这些流实际输出的是什么,我可以试试。如果我直接在shell上运行该命令,它将打印完整的输出。我只从Python脚本中看到了这个问题。我将把输出写入一个文件,然后再试一次。如何将命令stdout/stderr重定向到Python之外的文件?上面所有命令的输出都可以存储在一个变量中,然后我将变量输出写入一个文件?这不是和将该变量的值打印到stdout的结果相同吗?您的shell是windows/bash/linux shell吗?你的命令是cygwin命令吗?你真的应该试着不要使用
shell=True
,因为一开始它似乎让事情变得更容易,但最终你会遇到更多问题…@Jean-Françoisfare shell是bash,操作系统是FreeBSD。我不确定这是否重要,但我通过SSH连接到服务器。我将SSH连接到服务器并从那里运行脚本。Python的版本是:2.7.3有时主进程会部署一个子进程,因此无法捕获所有输出。我建议您将输出捕获到一个文件中,然后重新读取该文件。我可以发布一个答案,但我不确定是否是这样。