The following is an example of how to use Python to call the System Shell in Linux.

Source: Internet
Author: User
Tags call shell echo command linux shell commands

The following is an example of how to use Python to call the System Shell in Linux.

You need to directly call Shell commands to complete some simple operations, such as mount a file system. So how can we use Python to call Linux Shell commands? The following describes several common methods:
1. OS Module

1.1. exec method family of OS Module
The exec System Method of Python is the same as that of Unix exec system. These methods are applicable when an external program is called in a child process, because the external program replaces the code of the current process and does not return. (I have read the help (OS) --> search "exec" introduction, but I don't quite understand how to use it)


1.2. system method of the OS Module
The system method creates a sub-process to run the external program. The method returns only the running results of the external program. This method is applicable when no external program outputs results.

>>> Import OS >>> OS. system ("echo \" Hello World \ "") # directly use OS. system calls an echo command Hello World ------> Print the command result 0 ------> What's this? Return Value? >>> Val = OS. system ("ls-al | grep \" log \"") # Use val to receive the returned value-rw-r -- 1 root 6030829 Dec 31 log ------> at this time, only the command result is printed >>> print val 0 ------> note, when the command runs normally, the return value is 0 >>> val = OS. system ("ls-al | grep \" log1 \ "") >>> print val 256 ------> Use OS. system calls a command that does not return results. The returned value is 256 ~ >>>

Note: As mentioned above, fat will be the result of an external program, that is, the result of OS. system ~

1.3. popen method of OS Module
This method is useful when an external program output result is required. A class file object is returned, and the read () or readlines () method of this object can be called to read the output content. For example, when using urllib to call a Web API, you need to process the obtained data. OS. popen (cmd) to get the output content of the command, you only need to call read () or readlines (), such as a = OS. popen (cmd). read ()

>>> OS. popen ('LS-lt ') # Call OS. popen (cmd) does not get the expected results <open file 'LS-lt ', mode 'R' at 0xb7585ee8 >>>> print OS. popen ('LS-lt '). read () # Call the read () method to obtain the command Result total 6064-rwxr-xr-x 1 long 23 Jan 5 hello. sh-rw-r -- 1 long 147 Jan 5 Makefile drwxr-xr-x 3 long 4096 Jan 2 test-rw-r -- 1 root 6030829 dec 31 log drwxr-xr-x 2 long 4096 Dec 28 pip_build_long drwx ------ 2 Debian-TPD 4096 Dec 23 pulse-gylJ5EL24GU9 drwx ------ 2 long 4096 Jan 1 1970 orbit-long> val = OS. popen ('LS-lt '). read () # Use variables to receive command return values> if "log" in val: # We can use in to determine that the returned value contains a string... print "Haha, there is the log "... else :... print "No, not happy "... haha, there is the log

2. commands Module

The getoutput method of the commands module is used. The difference between this method and popend is that popen returns a class object, and this method returns the output result of an external program as a string, in many cases, it is easier to use it.
Main Methods:
* Commands. getstatusoutput (cmd) returns (status, output)
* Commands. getoutput (cmd) returns only the output results.
* Commands. getstatus (file) returns the execution result string of the ls-ld file and calls getoutput. This method is not recommended.

Long @ zhouyl:/tmp/tests $ python Python 2.7.3 (default, Jan 2 2013, 16:53:07) [GCC 4.7.2] on linux2 Type "help", "copyright ", "credits" or "license" for more information. >>> import commands >>> commands. getstatusoutput ('LS-lt ') # Return (status, output) (0, 'total 5900 \ n-rwxr-xr-x 1 long 23 Jan 5 hello. sh \ n-rw-r -- 1 long 147 Jan 5 Makefile \ n-rw-r -- 1 long 6030829 Jan 5 2 Log')> commands. getoutput ('LS-lt ') # Return the output result of the command (it seems that the output format is different from that of the Shell command ~) 'Total 5900 \ n-rwxr-xr-x 1 long 23 Jan 5 hello. sh \ n-rw-r -- 1 long 147 Jan 5 Makefile \ n-rw-r -- 1 long 6030829 Jan 5 log' >>> commands. getstatus ('log') # Call commands. the command in getoutput performs the same operation on the 'log' File '-rw-r -- 1 long 6030829 Jan 5 log' >>>

3. subprocess Module

According to the official Python documentation, the subprocess module is used to replace the above modules. There is a parallel ssh tool-mssh implemented using Python. The code is short, but interesting. It calls the subprocess promoter process in the thread to work.
>>> From subprocess import call
>>> Call (["ls", "-l"])

The advantage of subprocess over system is that it is more flexible (you can get standard output, standard errors, "real" status code, better error handling, etc ..). I think the use of OS. system is outdated or about to expire.

4. Comparison and summary of the Zhongfang Method
4.1. About OS. system
OS. system ("some_command with args") passes commands and parameters to your system shell, which is good, this is because you can run multiple commands at the same time and set pipelines and input/output redirection. For example:
OS. system ("some_command <input_file | another_command> output_file ")
However, although this is convenient, you need to manually handle shell escape characters, such as spaces. In addition, this only allows you to run simple shell commands and not external programs.

4.2. About OS. popen
Use stream = OS. popen ("some_command with args") can also work with OS. the same thing as OS. system is different from OS. popen returns a class object that is used to access standard input and output.
4.3. About subprocess. popen
The Popen class of the subprocess module is intended to replace OS. popen, but it is a little more complex than OS. popen because it is comprehensive.
For example, you can use print Popen ("echo Hello World", stdout = PIPE, shell = True ). stdout. read () to replace print OS. popen ("echo Hello World "). read (). However, in comparison, it is good to use a unified class including four different popen functions.

4.4. About subprocess. call
The call function of the subprocess module. It is basically like the Popen class and uses the same parameters, but it simply waits for the command to complete and returns code to you. For example:

return_code = subprocess.call("echo Hello World", shell=True)


The OS module also has the fork/exec/spawn functions in C, but I do not recommend using them directly. Subprocess may be more suitable for you.

Python and shell read a row of a file

Python and shell (awk command) can directly read a row of the file and read it by row number. It can accurately obtain a field of the row, which is similar to the operation of locating a point on the X axis and Y axis.

1. awk takes a column value in a row

Awk can set conditions to output the specified k fields in each row from Row m to row n in the file. The format is as follows:

awk  'NR==m,NR==n {print $k}' path/filename

M, n, k indicates the actual value. If you want to use a variable to represent the value of m and n, the variable must be enclosed in single quotes. NR, {print} is the required field of the awk command in this usage. path/filename indicates the path and file name of the file to be read. Two rows are specified here. If only one row is specified, write as follows:

awk  'NR==m {print $k}' path/filename

2. python retrieves a column from a row

The linecache module provided by the Standard Library provides a specific method for retrieving a row:

import linecachetheline = linecache.getline(filepath, line_number)

After obtaining the relevant rows, split the theline into a list, and then set the value of the list index. For example, theline. split () [2].

Iii. usage of the linecache Module

Now, the linecache module is mentioned. Other methods of linecache are listed here. The linecache module allows any row from any file and uses the cache for optimization. It is common to read multiple rows from a single file.

Linecache. getlines (filename) Get all content from the file named filename, output as List format, one element in the file per behavior list, and store the linenum-1 as the element in the list
Linecache. getline (filename, lineno) obtains line lineno from the file named filename. This function will never throw an exception-when an error is generated, it will return "(The linefeed will be included in the row found ). If the file is not found, this function will be searched in sys. path.
Linecache. receivache () clears the cache. If you no longer need the rows previously obtained from getline ()
Linecache. checkcache (filename) checks the validity of the cache. If the files in the cache change on the hard disk and you need to update the version, use this function. If filename is omitted, all entries in the cache are checked.
Linecache. updatecache (filename) updates the cache file named filename. If the filename file is updated, you can use this function to update the list returned by linecache. getlines (filename.
Example:

# cat a.txt
1a2b3c4d5e6f7g

1. Obtain the.txt file.

>>> a=linecache.getlines('a.txt')>>> a['1a\n', '2b\n', '3c\n', '4d\n', '5e\n', '6f\n', '7g\n']

2. Obtain the contents of line 1-4 in the.txt file.

>>> a=linecache.getlines('a.txt')[0:4]>>> a['1a\n', '2b\n', '3c\n', '4d\n']

3. Obtain the contents of Line 1 in the.txt file.

>>> a=linecache.getline('a.txt',4)>>> a'4d\n'

Note:

After you use linecache.getlines('a.txt 'character to open the file content, if the.txt file changes, for example, if you use linecache. getlines again to obtain the content, not the latest content of the file, or the previous content, there are two methods:

The latest content of the token;

2. directly use linecache.updatecache('a.txt 'to obtain the latest a.txt content.

After reading a file, you do not need to use the File Cache. You need to clear the cache at the end so that linecache. cacache () can clear the cache and release the cache.

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.