Python 3 Run shell command

Source: Internet
Author: User

#python 3.5, Win10

Introduction Package

#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)

When #universal_newlinews is True, the input is the STR stream, (default) is False when the byte stream

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'll break at P1|p2, because the before Stdout|stderr p Ipe would be extract and not use anymore

#if call P1|2.communicate () [0|1] Before P3 = constructor, would 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 (), 'll 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) would 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 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 Run shell command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.