如何在Python中使用子流程

如何在Python中使用子流程,python,subprocess,Python,Subprocess,我想用python执行以下shell命令:grep'string'文件| tail-1 | cut-c1-3 我试过: import subprocess i = 1 while i < 1070: file = "sorted." + str(i) + ".txt" string = "2x" subprocess.call(grep 'string' file | tail -1 | cut -c 1-3) i = i + 1 导入子流程 i=1 当

我想用python执行以下shell命令:
grep'string'文件| tail-1 | cut-c1-3

我试过:

import subprocess

i = 1
while i < 1070:
    file = "sorted." + str(i) + ".txt"
    string = "2x"
    subprocess.call(grep 'string' file | tail -1 | cut -c 1-3)
    i = i + 1
导入子流程
i=1
当i<1070时:
file=“sorted.”+str(i)+“.txt”
string=“2x”
子进程调用(grep'string'文件| tail-1 | cut-c1-3)
i=i+1

任何帮助都将不胜感激。谢谢。

子流程希望参数为字符串或数组:

subprocess.call("grep '{}' {} | tail -1 | cut -c 1-3".format(string, file), shell=True)
shell=True
是必需的,因为您使用的是特定于shell的命令,如管道

然而,在这种情况下,用纯python实现整个程序可能要容易得多


请注意,如果字符串或文件包含任何特殊字符(包括空格或引号),则该命令将不起作用,实际上可能会对系统造成各种不必要的影响。如果您需要它来处理这些简单的值,请考虑纯Python解决方案,设置<代码> Shell=false < /Cord>,并使用带有手动管道的数组语法,或者某种形式的逃逸。

< P>首先,无论您通过什么到“代码>子进程”。代码中未定义名称
grep
file
tail
cut
,您需要将整个表达式转换为字符串。由于grep命令的搜索字符串应该是动态的,所以在将其作为参数传递到函数之前,需要构造最终字符串

import subprocess

i = 1
while i < 1070:
    file = "sorted." + str(i) + ".txt"
    string = "2x"
    command_string = 'grep {0} {1} | tail -1 | cut -c 1-3'.format(string, file)
    subprocess.call(command_string)
    i = i + 1
编辑:以下是有关如何按照注释中的要求将数据存储到文本文件中的信息

import subprocess

outputs = []

i = 1
while i < 1070:
    file = "sorted." + str(i) + ".txt"
    string = "2x"
    command_string = 'grep {0} {1} | tail -1 | cut -c 1-3'.format(string, file)

    p = subprocess.Popen(command_string, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    stdoutdata, stderrdata = p.communicate()

    # stdoutdata now contains the output of the shell commands and you can use it
    # in your program, like writing the output to a file.

    outputs.append(stdoutdata)

    i = i + 1


with open('output.txt', 'w') as f:
    f.write('\n'.join(outputs))
导入子流程
输出=[]
i=1
当i<1070时:
file=“sorted.”+str(i)+“.txt”
string=“2x”
命令字符串='grep{0}{1}| tail-1 | cut-c1-3'。格式(字符串,文件)
p=subprocess.Popen(命令字符串,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True)
stdoutdata,stderdata=p.communicate()
#stdoutdata现在包含shell命令的输出,您可以使用它
#在程序中,例如将输出写入文件。
outputs.append(stdoutdata)
i=i+1
将open('output.txt','w')作为f:
f、 写入('\n'.连接(输出))

您的命令应作为字符串提供。 此外,如果要获取命令的输出,可以使用以下命令:

subprocess.run("grep 'string' file | tail -1 | cut -c 1-3", shell=True, capture_output=True, check=True)

其中,
capture\u output
(适用于Python3.7+)返回带有
returncode
stdout
stderr
的对象,如果您的命令失败,
check
标志将引发异常。

您查看了如何传递命令的任何地方吗?例如这里;此命令将搜索名为
file
的文件。要根据给定的代码对其进行调整,请改用
subprocess.call(“grep'string'{}tail-1 | cut-c1-3”.format(file),shell=True)
注意,对于不受信任的
字符串
文件
@mousetail,在具有一些用户输入的实际应用程序中,此命令很容易受到shell注入的攻击,在传入数据之前,需要对其进行验证和清理。这将返回
AttributeError:module'subprocess'没有属性“communicate”
@yellowcab True,该方法来自Popen对象。现在已修复。我想将结果打印到文本文件中。我怎么能这么做?
subprocess.run("grep 'string' file | tail -1 | cut -c 1-3", shell=True, capture_output=True, check=True)