pythonsubprocess.call和管道

2024-10-01 17:23:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个我想用的脚本subprocess.call执行一系列shell命令,但在执行时似乎忽略了一些命令。在

具体来说:

#!/usr/bin/python
import tempfile
import subprocess
import os
import re


grepfd, grepfpath = tempfile.mkstemp(suffix=".xx")
sedfd,  sedfpath  = tempfile.mkstemp(suffix=".xx")

# grepoutfile = open( grepfpath, 'w')
sedoutfile  = open( sedfpath,  'w' )

subprocess.call(['cp','/Users/bobby/Downloads/sample.txt', grepfpath])

sedcmd = [ 'sort', 
           grepfpath,
           '|', 
           'uniq',
           '|',
           'sed',
           '-e',
           '"s/bigstring of word/ smaller /"',
           '|',
           'column',
           '-t',
           '-s',
           '"=>"' ]

print "sedcmd = ", sedcmd
subprocess.call( ['ls', grepfpath ] )
subprocess.call( ['sort', '|', 'uniq' ], stdin = grepfd )
subprocess.call( sedcmd,  stdout = sedoutfile )

它会生成以下输出:

Pythond3.py

sedcmd=['sort',/var/folders/3h/\u 0xwt5bx0hx8tgx06cmq9h_4f183ql/T/tmp5Gp0ff.xx','|','uniq','|','sed','-e','“s/bigstring of word/smaller/”,''124;','列','-t','-s','=>;''] /var/folders/3h/_0xwt5bx0hx8tgx06cmq9h_4f183ql/T/tmp5Gp0ff.xx 排序:打开失败:|:没有这样的文件或目录
排序:无效选项--e 有关详细信息,请尝试“sort--help”。在

第一个'sort:open失败:|:没有这样的文件。。。来自第一个子进程调用['sort','|','uniq'],stdin=grepfd) 'sort:无效选项--e。。来自第二个子进程调用(sedcmd)。在

我见过很多在这种情况下使用管道的例子——那么我做错了什么呢?
谢谢!在


Tags: import命令opencalltempfilesortsuffixsubprocess
2条回答

该类将使用任意数量的管道运行命令:

管道.py

import shlex
import subprocess

class Pipeline(object):
    def __init__(self, command):
        self.command = command
        self.command_list = self.command.split('|')
        self.output = None
        self.errors = None
        self.status = None
        self.result = None

    def run(self):
        process_list = list()
        previous_process = None
        for command in self.command_list:
            args = shlex.split(command)
            if previous_process is None:
                process = subprocess.Popen(args, stdout=subprocess.PIPE)
            else:
                process = subprocess.Popen(args,
                                           stdin=previous_process.stdout,
                                           stdout=subprocess.PIPE)
            process_list.append(process)
            previous_process = process
        last_process = process_list[-1]
        self.output, self.errors = last_process.communicate()
        self.status = last_process.returncode
        self.result = (0 == self.status)
        return self.result

此示例演示如何使用类:

线束.py

^{pr2}$

我为测试创建了以下示例文件:

示例.txt

word1>word2=word3
list1>list2=list3
a>bigstring of word=b
blah1>blah2=blah3

输出

a       smaller   b
blah1  blah2      blah3
list1  list2      list3
word1  word2      word3

因此,如果要在命令中使用shell管道,可以在子进程中添加shell=True: 所以会是这样的:

sedcmd = 'sort /var/folders/3h/_0xwt5bx0hx8tgx06cmq9h_4f183ql/T/tmp5Gp0ff.xx | uniq | sed -e "s/bigstring of word/ smaller /" | column -t -s "=>" '
subprocess.call(sedcmd, shell=True)

但是小心shell=True,强烈不鼓励使用它:subprocess official documentation

因此,如果要使用不带shell=True的管道,可以使用子程序.管道在stdout中,下面是一个如何执行的示例:stackoveflow answer

相关问题 更多 >

    热门问题