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.* ...
随机推荐
- 如何在Ubuntu中安装中文输入法
在使用ubuntu系统时,有的时候总觉得英文输入法不方便操作,总希望能有中文输入法可以辅助操作,那怎样才能在ubuntu中安装中文输入法呢?下面有一种简单的方法可以安装中文输入法. 如何在ubuntu ...
- fast-rcnn里的一些具体内容
NMS:Non-Maximum Suppression(非极大值抑制) 假设从一个图像中得到了2000个region proposals,通过在RCNN和SPP-net之后我们会得到2000*4096 ...
- ubuntu gnome桌面隐藏顶栏
注意:ubuntu 14.04.5默认的为unity桌面,有多点触发,没有自带Tweak Tool工具.需安装gnome 桌面,可参见我的另一随笔. 环境: ubuntu 14.04.5 gnome ...
- 玩转X-CTR100 l STM32F4 l AT24C02 EEPROM存储
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器 板载EEP ...
- springboot学习章节-spring常用配置
1.Scope package com.zhen.highlights_spring4.ch2.scope; import org.springframework.context.annotation ...
- 201621123001《Java程序设计》第1周学习总结
1. 本周学习总结 认识java的三个层次:java语法 面向对象设计能力 应用 . 学习eclipse创建java文件的方法. 学习markdown的基本语法,了解写博客的几种常用形式. 了解JVM ...
- AOP 实现请求参数打印
1.编写打印方法 import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import org.aspe ...
- redis 五大数据类型之set篇
1.sadd/smembers/sismember --set集合赋值 查看值, --sismember 是查看set集合是否有指定的值,有返回1 没有返回0 2.scard,获取集合里面的元素个数 ...
- 转--让一个运行在SYSTEM权限下的进程与当前用户的桌面进行交互
#define DESKTOP_ALL ( DESKTOP_READOBJECTS | DESKTOP_CreateWINDOW | \ DESKTOP_CreateMENU | DESKTOP_HO ...
- chromium ⑤
我们都知道chromium是用webkit完成页面显示的, 那么chromium是怎样集成和封装webkit的呢? 是怎样将webkit整合到自己的框架中,并将一个页面渲染出来的? 这篇我 ...