#python 3.5 , win10

引入包

#os.chdir('path')

import os
import subprocess

#https://docs.python.org/3.5/library/subprocess.html?highlight=subprocess#module-subprocess

#http://ltp.readthedocs.io/zh_CN/latest/ltptest.html

Run 1 process

p1 = subprocess.Popen('cws_cmdline --input input_file.txt ',stdout=subprocess.PIPE,stderr=subprocess.PIPE [,universal_newlines=True])

#p1 = subprocess.Popen(['cws_cmdline','--input', 'input_file.txt '],stdout=subprocess.PIPE,stderr=subprocess.PIPE)

#universal_newlinews 为 True 时,输入为 str 流,(默认)为 False 时为 byte 流

output_10 = p1.communicate()[0]  #stdin
output_11 = p1.communicate()[1]  #stderr

Run pipe-line

p1 = subprocess.Popen('cws_cmdline --input input_file.txt ',stdout=subprocess.PIPE,stderr=subprocess.PIPE)

p2 = subprocess.Popen('pos_cmdline --input no_file.txt ',stdin=p1.stdout,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

p3 = subprocess.Popen('ner_cmdline --input no_file.txt ',stdin=p2.stdout,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

#if call p1|2.communicate()[0|1] before p3.communicate(), pipeline will break at p1|p2, because the before stdout|stderr pipe will be extract and not use anymore

#if call p1|2.communicate()[0|1] before p3 = constructor,  will get ValueError: I/O operation on closed file

output30 = p3.communicate()[0]  #stdout
output31 = p3.communicate()[1]  #stderr

#if call p1|2.communicate()[0|1] after p3.communicate(), will get close warning.

'''  def communicate(self, input=None, timeout=None):

#...

if self.stdin:
                self._stdin_write(input)
            elif self.stdout:
                stdout = self.stdout.read()
                self.stdout.close()
            elif self.stderr:
                stderr = self.stderr.read()
                self.stderr.close()
            self.wait()

#...

'''

Run process one by one

#px.communicate()  actually run the pipe line until px, all the pipe content(p1&p2&p3.stdin&stdout&stderr pipe) will be extract and not usable any more .

#if you want to save each pipe line node's meadian content , you need to use a new pipe as   stdin=subprocess.PIPE , and use  communicate('input Popen's stdin content')

p1 = subprocess.Popen('cws_cmdline --input input_file.txt ',stdout=subprocess.PIPE,stderr=subprocess.PIPE)

output_10 = p1.communicate()[0]
output_11 = p1.communicate()[1]

str_10 = output_10.decode('utf-8')

str_11 = output_10.decode('utf-8')

p2 = subprocess.Popen('pos_cmdline --input no_file.txt ',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

#communicate('input Popen's stdin content once if and only if stdin==subprocess.PIPE')

output_20 = p2.communicate( bytes(str_10, encoding = 'utf-8') )[0]
output_21 = p2.communicate()[1]

# for px.communicate(), only the first time call can you set the input

#or use  "universal_newlines=True" for Popen() to process str stream

p3 = subprocess.Popen('ner_cmdline --input no_file.txt ',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

output_3 = p3.communicate( output20)
[output_30, output_31] = output_3

Python 3 运行 shell 命令的更多相关文章

  1. Linux系统下python代码运行shell命令的方法

    方法一:os.popen #!/usr/bin/python # -*- coding: UTF-8 -*- import os, sys # 使用 mkdir 命令 a = 'ls' b = os. ...

  2. python中执行shell命令行read结果

    +++++++++++++++++++++++++++++ python执行shell命令1 os.system 可以返回运行shell命令状态,同时会在终端输出运行结果 例如 ipython中运行如 ...

  3. 让你提前认识软件开发(23):怎样在C语言中运行shell命令?

    第1部分 又一次认识C语言 怎样在C语言中运行shell命令? [文章摘要] Linux操作系统具备开源等诸多优秀特性,因此在很多通信类软件(主流开发语言为C语言)中,开发平台都迁移到了Linux上, ...

  4. PHP 反引号运行Shell命令,C程序

    /********************************************************************* * PHP 反引号运行Shell命令,C程序 * 说明: ...

  5. python中执行shell命令的几个方法小结(转载)

    转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...

  6. java运行shell命令,chmod 777 xxx,改变权限无效的解决的方法。

    在java程序中运行shell命令,改变文件的权限.能够在命令行中运行 chmod 777 <span style="font-family: Arial, Helvetica, sa ...

  7. 「Python」6种python中执行shell命令方法

    用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...

  8. Python 分页和shell命令行模式

    前言 除了手动添加你的文章后外,你还可以用命令行来添加,python 自带了一种命令行 就是 shell 快速添加博文:Shell命令行模式 在你的目录下:mysite python manage.p ...

  9. python(6)-执行shell命令

    可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*           --废弃 commands.* ...

随机推荐

  1. learning ddr init power-up initialization sequence

  2. 认识微软Visual Studio Tools for AI

    认识微软Visual Studio Tools for AI   微软已经发布了其 Visual Studio Tools for AI 的测试版本,这是微软 Visual Studio 2017 I ...

  3. day15 装饰器

    关于函数的装饰器 1 .装饰器,(难点,重点) 开闭原则: 对功能的扩展开放 对代码的修改是封闭 通用装饰器语法: def wrapper(fn): def inner(*args,**kwargs) ...

  4. PyCharm进行远程开发和调试linux服务器

    简介: 或许我也应该迁移到linux环境去开发. 最近写的一些小东西,在wnidows上开发,在windows上调试,都很正常.可是一旦放进linux服务器,就歇菜了. 那么我们有什么办法处理这个wi ...

  5. set,pair容器使用方法

    题目链接:http://codeforces.com/gym/100989/problem/D In this cafeteria, the N tables are all ordered in o ...

  6. <Codis><JedisPool><DeadLock>

    Overview Background: I start a thread [call thread A below]in Spark driver to handle opening files i ...

  7. 关于urls 的基础

    1 普通正则 2 分组正则 url(r'/blog/(\d+)/(\d+)',views.blog)     blog(request,arq1,arq2) 按照位置传参 3 分组命名 url(r'/ ...

  8. xampp 忘记密码的处理方式.

    网上看到一些方法: 大部分是第一种:  方法一 这个方法, 我使用的时候没有生效. -------------- 后来看到另外一种方法 .  直接替换user表的三个文件.  这个方法成功了. xam ...

  9. kbmMW功能#5 - kbmMWProcess单元

    在新的kbmMW v.5.06.20版本中新加kbmMWProcess单元.通过TkbmMWProcess类的各种类方法,可以轻松地在Windows上对外部进程进行分组,启动和停止.在即将发布的小修补 ...

  10. asp.net 后台执行js

    1. 用Response.Write方法 代码如下: Response.Write("<script type='text/javascript'>alert("XXX ...