Python 3 运行 shell 命令
#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 命令的更多相关文章
- Linux系统下python代码运行shell命令的方法
方法一:os.popen #!/usr/bin/python # -*- coding: UTF-8 -*- import os, sys # 使用 mkdir 命令 a = 'ls' b = os. ...
- python中执行shell命令行read结果
+++++++++++++++++++++++++++++ python执行shell命令1 os.system 可以返回运行shell命令状态,同时会在终端输出运行结果 例如 ipython中运行如 ...
- 让你提前认识软件开发(23):怎样在C语言中运行shell命令?
第1部分 又一次认识C语言 怎样在C语言中运行shell命令? [文章摘要] Linux操作系统具备开源等诸多优秀特性,因此在很多通信类软件(主流开发语言为C语言)中,开发平台都迁移到了Linux上, ...
- PHP 反引号运行Shell命令,C程序
/********************************************************************* * PHP 反引号运行Shell命令,C程序 * 说明: ...
- python中执行shell命令的几个方法小结(转载)
转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...
- java运行shell命令,chmod 777 xxx,改变权限无效的解决的方法。
在java程序中运行shell命令,改变文件的权限.能够在命令行中运行 chmod 777 <span style="font-family: Arial, Helvetica, sa ...
- 「Python」6种python中执行shell命令方法
用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...
- Python 分页和shell命令行模式
前言 除了手动添加你的文章后外,你还可以用命令行来添加,python 自带了一种命令行 就是 shell 快速添加博文:Shell命令行模式 在你的目录下:mysite python manage.p ...
- python(6)-执行shell命令
可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen* --废弃 popen2.* --废弃 commands.* ...
随机推荐
- [POJ2985]The k-th Largest Group
Problem 刚开始,每个数一个块. 有两个操作:0 x y 合并x,y所在的块 1 x 查询第x大的块 Solution 用并查集合并时,把原来的大小删去,加上两个块的大小和. Notice 非旋 ...
- ActiveMQ的安装与配置
ActiveMQ的安装与配置详情 (1)ActiveMQ的简介 MQ: (message queue) ,消息队列,也就是用来处理消息的,(处理JMS的).主要用于大型企业内部或与企业之间的传递数据信 ...
- sqlalchemy 模型中添加根据身份证号计算性别和年龄
class Users(Base, BaseMixin): username = Column(String(24)) id_Num = Column(String(18) ) # 身份证号码 @pr ...
- Java:下拉列表绑定后台数据
后台传进来一个List集合,存着某对象集合,将其显示在下拉列表 一.HTML代码 页面有个下拉列表,如图所示: <td style="width:30%"> <s ...
- python 定义class时的内置方法
__contains__():对类实例使用in ,not in操作时调用 class A(object): def __init__(self,num): self.num=num def __con ...
- base64加密解密c++代码
关于base64加密解密代码: 程序运行功能请自行查看main函数: #include <stdio.h> #include <string.h> #include <a ...
- [Leetcode 55]跳格子JumpGame
[题目] Given an array of non-negative integers, you are initially positioned at the first index of the ...
- node 慕课笔记
global global.testVar2 = 200; 在别的文件中可以任意调用到 因为global是全局变量相当于js的window一样的
- GNU C的定义长度为0的数组
在标准C和C++中,长度为0的数组是被禁止使用的.不过在GNU C中,存在一个非常奇怪的用法,那就是长度为0的数组,比如Array[0];很多人可能觉得不可思议,长度为0的数组是没有什么意义的,不过在 ...
- 在 Andriod/IOS 程序中使用自定义字体
很早就遇到这个问题,QDAC作者也在这里给出了方案.