Python-subprocess, Glob, and Shlex modules

Source: Internet
Author: User
Tags glob stdin

One, subprocess module

1.subprocess and commonly used encapsulation functions

When we run Python, we are all creating and running a process. Like the Linux process, a process can fork a child process and let the child process exec another program. In Python, we fork a child process through the subprocess package in the standard library and run an external program.

The subprocess package defines a number of functions that create child processes, each of which creates child processes in different ways, so we can choose one to use as needed. In addition, Subprocess provides some tools for managing standard streams and pipelines (pipe) to use text communication between processes.


Subprocess.call ()

Parent process waits for child process to complete

Return exit information (ReturnCode, equivalent to Linux exit code)


Subprocess.check_call ()

Parent process waits for child process to complete

Returns 0

Check the exit information, and if ReturnCode is not 0, cite error subprocess. Calledprocesserror, this object contains the ReturnCode property, which can be try...except ... To check

Cases:

#!/usr/bin/env pythonimport Subprocesstry:subprocess.check_call ("Exit 1", shell=true) except subprocess. Calledprocesserror:print "Call fail" except Exception,e:print eprint "Hello,china"

Operation Result:

Call fail

Hello,china


Subprocess.check_output ()

Parent process waits for child process to complete

Returns the output of the child process to the standard output

Check the exit information, and if ReturnCode is not 0, cite error subprocess. Calledprocesserror, which contains the ReturnCode property and the output property, outputs a result of the standard output, available try...except ... To check


Cases:

#!/usr/bin/env Pythonimport Subprocessretcode = subprocess.check_output (["LS", "-l"]) #列表一般不带shellprint Retcode

Operation Result:

-rw-r--r--1 root root 94 Jan 22:09 001.py

-rw-r--r--1 root root 255 Jan 22:19 002.py

-rw-r--r--1 root root 101 Jan 22:59 003.py


Description

Place the program name (LS) with the parameter (-L) in a list and pass it to Subprocess.check_output ();

The shell defaults to False, under Linux, when Shell=false, Popen calls OS.EXECVP () to execute the program specified by args, or shell=true, if args is a string, Popen directly invokes the system's shell to execute the program specified by args, and if args is a sequence, the first item of args is the definition of the program command string, and the other is additional parameters when invoking the system shell.


or example:

#!/usr/bin/env Pythonimport Subprocessretcode = Subprocess.check_output ("Ls-l", Shell=true) #一般带参数, add Shellprint Retcode


Subprocess. Popen ()

Class Popen (args, bufsize=0, Executable=none, Stdin=none, Stdout=none, Stderr=none, Preexec_fn=none, Close_fds=false, Shell=false, Cwd=none, Env=none, Universal_newlines=false, Startupinfo=none, creationflags=0)


After the Popen object is created, the main program does not automatically wait for the child process to complete. We must call the object's Wait () method before the parent process waits (that is, block block)

Example 1 (parent process does not wait for child processes):

#!/usr/bin/env pythonimport subprocesschild = subprocess. Popen ("Ping-c 3 www.baidu.com", shell=true) print "Hello,china"

Operation Result:

Hello,china

[Email protected] python]# PING www.a.shifen.com (14.215.177.38), bytes of data.

Bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=56 time=7.45 ms

Bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=56 time=7.29 ms

Bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=56 time=8.05 ms


Description

The parent process does not wait for the child to complete after it has been opened, but runs print directly


Example 2 (parent process waits for child process):

#!/usr/bin/env pythonimport subprocesschild = subprocess. Popen ("Ping-c 3 www.baidu.com", Shell=true) child.wait () print "Hello,china"

Operation Result:

PING www.a.shifen.com (14.215.177.39) bytes of data.

Bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=56 time=5.49 ms

Bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=56 time=5.69 ms

Bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=56 time=6.66 ms


---www.a.shifen.com ping statistics---

3 Packets transmitted, 3 received, 0% packet loss, time 2004ms

RTT Min/avg/max/mdev = 5.496/5.950/6.664/0.514 ms

Hello,china


Other actions of the parent process to the child process:

Child.poll () # Check child process status

Child.kill () # terminating child process

Child.send_signal () # Send a signal to a child process

Child.terminate () # terminating child process

Child.pid #存储子进程的PID



2. Text flow control for child processes

The standard input, standard output, and standard errors for child processes are represented by the following attributes:

Child.stdin

Child.stdout

Child.stderr

You can change standard input, standard output, and standard errors when Popen () is established, and can take advantage of subprocess. Pipes connect the inputs and outputs of multiple sub-processes to form pipelines (pipe), and, if not written stdin and stdout, default to print child process execution results to the screen instead of saving in memory


Example 1:

#!/usr/bin/env pythonimport subprocesschild = subprocess. Popen (["LS", "-L"],stdout=subprocess. PIPE) Print Child.stdout.read ()

Operation Result:

-rw-r--r--1 root root 94 Jan 22:09 001.py

-rw-r--r--1 root root 255 Jan 22:19 002.py

-rw-r--r--1 root root 149 Jan 00:47 003.py


Example 2:

#!/usr/bin/env pythonimport subprocesschild1 = subprocess. Popen (["Cat", "/etc/passwd"],stdout=subprocess. PIPE) child2 = subprocess. Popen (["grep", "/bin/bash"],stdin=child1.stdout,stdout=subprocess. PIPE) out = Child2.communicate () print out

Operation Result:

R (' root:x:0:0:root:/root:/bin/bash\nzabbix:x:1001:1001::/home/zabbix:/bin/bash\nelk:x:1002:1002::/home/elk:/ Bin/bash\nmemcached:x:1003:1003::/home/memcached:/bin/bash\n ', None)


Description

Subprocess. Pipe piping actually provides a buffer for text flow. The child1 stdout the text out to the buffer, and then Child2 's stdin reads the text from the pipe. The output text of the child2 is also stored in the pipe until the communicate () method reads the text in the pipe from the pipe, and communicate () is a method of the Popen object that blocks the parent process until the child process finishes ; Child2.communicate () is equivalent to the three methods of Child2.write (), Child2.close (), Child2.read ()




Second, Glob module

A shell wildcard under Python that lets you find file path names that match a specific rule, similar to a file search under Windows. Only three match characters are used: "*", "?", "[]". "*" matches 0 or more characters, "?" Matches a single character, "[]" matches a character within a specified range.


1.glob.glob

Returns a list of all matching file paths. There is only one parameter pathname, which defines the file path matching rules, which can be either absolute or relative path

Cases:

In [1]: Import globin [2]: Glob.glob ("./*.py") out[2]: ['./20.py ', './1.py ',


2.glob.iglob

Instead of Glob.glob, get a traversal object that can be used to get a matching file path name



Three, Shlex module

Shlex.split (s[, comments[, POSIX])

Using a shell-like syntax to split the string s, the default is separated by a space, and shlex.split () can recognize the quotation marks as an element in the quotation marks.

Cases:

In [7]: Import Shlexin [8]: shlex.split (' Hello ', "word ') out[8]: [' Hello,word ']


Python-subprocess, Glob, and Shlex modules

Related Article

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.