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

时间: 2024-07-28 19:15:30

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

Linux下的C程序,使用函数execlp运行Shell命令

通过C程序运行Shell命令,使用execlp函数 execlp的几个参数分别为:文件名,各个参数,(char*)0 其中"各个参数"部分中的第一个参数就是文件名(相当于文件名要输入两遍) 最后一个参数写(char*)0就可以了 文件b.cpp代码: 循环地输入命令→执行命令,直到按下Ctrl+C结束 #include <stdio.h> #include <string.h> #include <unistd.h> #include <std

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

在java程序中运行shell命令,改变文件的权限.能够在命令行中运行 chmod 777 <span style="font-family: Arial, Helvetica, sans-serif;">/data/misc/123.sh"</span> 来改变权限,可是在java代码中运行这个命令时使用 Runtime.getRuntime().exec("chmod 777 /data/misc/123.sh"): 无效,使用

python中得到shell命令输出的方法

python中得到shell命令输出的方法: 1. import subprocess output = subprocess.Popen(['ls','-l'],stdout=subprocess.PIPE,shell=True).commun icate() print output[0] 2. import commands return_code, output = commands.getstatusoutput('ls -l') 3. import os process = os.p

运行shell命令

首先将shell命令命名为.sh文件 将上面的代码保存为test.sh.并 cd 到对应文件夹: chmod +x ./test.sh #使脚本具有运行权限 ./test.sh #运行脚本 假设报错/bin/bash^M: bad interpreter: No such file or directory 出现上面错误的原因之中的一个是脚本文件是DOS格式的, 即每一行的行尾以\r\n来标识, 使用vim编辑器打开脚本, 执行: :set ff? 能够看到DOS或UNIX的字样. 使用set

C++/Php/Python 语言执行shell命令

编程中经常需要在程序中使用shell命令来简化程序,这里记录一下. 1. C++ 执行shell命令 1 #include <iostream> 2 #include <string> 3 #include <stdio.h> 4 5 int exec_cmd(std::string cmd, std::string &res){ 6 if (cmd.size() == 0){ //cmd is empty 7 return -1; 8 } 9 10 char

python中执行shell命令

sh是一个比subprocess好的库,能够执行shell命令 1.查看ip: [[email protected] myblog]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.31.253.193 netmask 255.255.240.0 broadcast 172.31.255.255 ether 00:16:3e:01:72:ac txqueuelen 1000 (Etherne

python如何执行shell命令

Jmeter是公司做接口测试的一个开源项目,今天在研究如何用python写个脚本去执行这个jmeter脚本,Jmeter有命令行模式和界面模式,设置好了环境之后,我用了最简单的一条命令做了测试: jmeter -n -t <testplan filename> -l <listener filename> 比如:jmeter -n -t  ..../文件.jmx  -l  result.txt  这里忽略jmx文件的编写,这些是测试写好的,我只要调用命令去执行就好了,后期写个定时任

python中使用shell命令及常用函数

操作: 导入模块 subprocess import subprocess zhi = subprocess.getoutput('ls') 1. subprocess模块中的常用函数 函数 描述 subprocess.run() Python 3.5中新增的函数.执行指定的命令,等待命令执行完成后返回一个包含执行结果的CompletedProcess类的实例. subprocess.call() 执行指定的命令,返回命令执行状态,其功能类似于os.system(cmd). subprocess

python之执行shell命令

[[email protected] ~]# python Python 2.7.5 (default, Sep 15 2016, 22:37:39)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> impor