Python: 使用 subprocess 和 Popen 打印输出
Python: Using subprocess and Popen to Print output
我有一个 python 程序,它会 cat 一个文件,然后仅当在输出中找到单词 "oranges" 时才打印输出。我想知道如果在输出中找到 "oranges",我该如何扭转这一点并让程序不输出任何内容?
#! /usr/bin/python
import commands, os, string
import sys
import fileinput
import subprocess
from subprocess import Popen, PIPE
import shlex
cmd = "cat /root/newfile.txt"
args = shlex.split(cmd)
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if out.find("oranges") > -1:
print out
怎么样:
if not "oranges" in out:
print out
我有一个 python 程序,它会 cat 一个文件,然后仅当在输出中找到单词 "oranges" 时才打印输出。我想知道如果在输出中找到 "oranges",我该如何扭转这一点并让程序不输出任何内容?
#! /usr/bin/python
import commands, os, string
import sys
import fileinput
import subprocess
from subprocess import Popen, PIPE
import shlex
cmd = "cat /root/newfile.txt"
args = shlex.split(cmd)
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if out.find("oranges") > -1:
print out
怎么样:
if not "oranges" in out:
print out