squirrel2300

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

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2022-01-02
  • 2021-12-21
  • 2021-12-21
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2023-03-20
  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案