Python calls the three generous methods of shell commands

Source: Internet
Author: User

Preface: Busy with the recent tasks, need to use some of the LIBSVM commands. such as in the terminal execution Java svm_train train_file model_file. Pythonsubset.py file Train_num train_file test_file and other commands. But file preparation is also written in Python. File must be in a format acceptable to LIBSVM. So use Python to write good features. Convert to LIBSVM acceptable format. Generate file, and then call training at the terminal. So I want to directly execute the command of the terminal directly inside the Python code. Bo Friends Blog Descriptive narrative very specific, here directly reproduced and do some staring.

#=====================================================

one, OS moduleExec method cluster for 1.1.os modules:

Ipython Interactive Interface:

In [1]: Import Osin [2]: os.execos.execl    os.execlp   os.execv    os.execvp   os.execle   os.execlpe  Os.execve   os.execvpe in  [2]: Os.execl? Type:        functionstring form: <function execl at 0xb73673e4>file:        /home/shifeng/anaconda/lib/python2.7/ Os.pydefinition:  

Os.exec+tab key Smart hint can see there are 8, help (OS.EXECL) and so can find its use method description.

System method for 1.2.os modules

The system method creates a child process to execute an external program. Method simply returns the execution result of the external program. 0 indicates successful execution.

In []: Os.system ("echo \" Hello world\ "") Hello Worldout[10]: 0In [All]: Os.system ("ls") all_roc_plot.py~  task1_ feature_all2.py  test.py   test.sh   ui_without_buy~scrapy_work  test test.py~  test.sh~out[11]: 0In [0]: Os.system ("Cat test.sh") echo "Hello World" out[12]: 0In []: Os.system ("sh test.sh") Hello Worldout[13]:
Above Some of the main shell commands, which are passed into the Os.system () parameter, can be run.

Just can't get the return value of the command.

1.3.os Module popen method

The Popen method can get the return value of the shell command. Os.popen (CMD), you need to call either read () or ReadLines (). Output results.


in [+]: Os.popen ("ls") out[14]: <open file ' ls ', mode ' R ' at 0xb67efd30>in []: Os.popen ("ls"). Read () out[15]: ' All_ Roc_plot.py~\nscrapy_work\ntask1_feature_all2.py\ntest\ntest.py\ntest.py~\ntest.sh\ntest.sh~\nui_without_buy~\ n ' in [+]: Os.popen ("ls"). ReadLines () out[16]: [' all_roc_plot.py~\n ', ' scrapy_work\n ', ' task1_feature_all2.py\n ', ' Test\n ', ' test.py\n ', ' test.py~\n ', ' test.sh\n ', ' test.sh~\n ', ' ui_without_buy~\n ']in []: s = os.popen ("ls"). Read () in [+]: For I in S.split ("\ n"): ....:     Print I ...   :     all_roc_plot.py~scrapy_worktask1_feature_ all2.pytesttest.pytest.py~test.shtest.sh~ui_without_buy~

Attention. After read () or ReadLines (), each of its elements includes a carriage return \ n.

Second, commands module

Using the GetOutput method of the Commands module, the difference between this method and Popend is that Popen returns a file handle, and this method returns the output result of the external program as a string. It is more convenient to use it in very many cases.
Main methods:

* Commands.getstatusoutput (CMD) return (status, output)
* Commands.getoutput (CMD) returns only output results
* Commands.getstatus (file) returns the run result string for Ls-ld file, calling GetOutput. It is not recommended to use this method

In [8]: Import commands in [9]: commands.getoutput ("ls") out[9]: ' all_roc_plot.py~\nscrapy_work\ntask1_feature_all2.py \ntest\ntest.py\ntest.py~\ntest.sh\ntest.sh~\nui_without_buy~ ' in [ten]: Commands.getstatusoutput ("ls") Out[10]: (0, ' All_roc_plot.py~\nscrapy_work\ntask1_feature_all2.py\ntest\ntest.py\ntest.py~\ntest.sh\ntest.sh~\nui_without_ Buy~ ')
three, subprocess module

Use the Subprocess module to create a new process. Ability to connect to the input/output/error pipelines of the new process. And be able to get the return status of the new process run. The purpose of using the Subprocess module is to replace old functions or modules such as Os.system (), os.popen* (), commands.*, and so on.

3.1.subprocess.call(["Some_command", "Some_argument", "Another_argument_or_path"])

Subprocess.call (Command,shell=true)

3.2.subprocess. Popen (command,shell=true)Suppose the command is not a running file. Shell=true cannot be saved.
Use the Subprocess module to create a new process that can be connected to the input/output/error pipelines of the new process. And be able to get the return status of the new process run. The purpose of using the Subprocess module is to replace old functions or modules such as Os.system (), os.popen* (), commands.*, and so on. The simplest way is to use classsubprocess. Popen (command,shell=true). The Popen class Popen.stdin,popen.stdout,popen.stderr three useful properties that enable communication with child processes.
In [all]: from subprocess import Callin []: Call (["LS", "-l"]) total consumption 40-rw-rw-r--1 Shifeng shifeng 3266 May 3 14:14 all_r Oc_plot.py~drwxrwxr-x 6 Shifeng shifeng 4096 June 16:59 scrapy_work-rw-rw-r--1 shifeng shifeng 12786 June 10:20 Task 1_feature_all2.pydrwxrwxr-x 2 Shifeng Shifeng 4096 June 8 11:36 test-rw-rw-r--1 Shifeng shifeng 3649 June 10:21 test. py-rw-rw-r--1 Shifeng Shifeng 255 June One 21:21 test.py~-rw-rw-r--1 shifeng shifeng 19 June 19:49 test.sh-rw-rw-r-  -1 Shifeng Shifeng 0 June 19:49 test.sh~-rw-rw-r--1 shifeng shifeng 0 April 8 22:15 ui_without_buy~out[12]: 0In []: From subprocess import Popenin [+]: Popen (["LS", "-l"]) out[14]: <subprocess.  Popen at 0xb67c91ac>in [15]: Total dosage 40-rw-rw-r--1 Shifeng shifeng 3266 May 3 14:14 all_roc_plot.py~drwxrwxr-x 6 Shifeng Shifeng 4096 June 16:59 scrapy_work-rw-rw-r--1 shifeng shifeng 12786 June 10:20 task1_feature_all2.pydrwxrwxr-x 2 S Hifeng Shifeng 4096 June 8 11:36 test-rw-rw-r--1 ShifengShifeng 3649 June 10:21 test.py-rw-rw-r--1 shifeng shifeng 255 June 21:21 test.py~-rw-rw-r--1 Shifeng Shifeng 19 June 19:49 test.sh-rw-rw-r--1 shifeng shifeng 0 June 19:49 test.sh~-rw-rw-r--1 shifeng shifeng 0 April 8 2 2:15 ui_without_buy~

1, Subprocess.call (command, shell=true) #会直接打印出结果.

2, subprocess. Popen (command, shell=true) can also be subprocess. Popen (command, stdout=subprocess. PIPE, shell=true) so you can output the results. Assuming the command is not a running file, Shell=true is not allowed to be omitted. Shell=true means the command is run under the shell.

#======================== under reprint ============

4. Comparison and summary of the public methods4.1. About Os.systemOs.system ("Some_command with args") passes the command and the number of parameters to your system shell。 This is good because you can execute multiple commands at the same time and be able to set up pipelines and input and output redirects . Example:
Os.system ("Some_command < Input_file | Another_command > Output_file ")
However, while this is convenient, you need to manually handle the escape of shell characters, such as spaces. In addition This will only allow you to execute simple shell commands and not execute external programs.
4.2. About Os.popenusing stream = Os.popen ("Some_command with args") can do the same thing as Os.system.。 Unlike Os.system, Os.popen will give you a file-like object so you can use it to access which program's standard input and output.

And There are three variants of popen that are slightly different in I/O processing. If you pass the whole thing through a string. Your commands are passed to the shell, assuming you pass them through a list. You don't have to worry about running away no matter what.
4.3.thePopen class for the Subprocess.popen subprocess module is intended as an alternative to Os.popen , but It's very comprehensive so it's slightly more complicated than Os.popen . Use up to learn Oh ~ ~.


For example, you can use print Popen ("Echo Hello World", Stdout=pipe, Shell=true). Stdout.read () to replace print os.popen ("Echo Hello World"). R EAD ().

In contrast, it is good to use a unified class that contains 4 different popen functions.
4.4. AboutThe call function of the Subprocess.call subprocess module.

It's basically like the Popen class and uses the same parameters, but it simply waits for the command to complete and gives you a return code. For example:
Return_code = Subprocess.call ("Echo Hello World", Shell=true)

#==========================================

References:

1. Bo Friends Blog: http://blog.csdn.net/longerzone/article/details/17889969

2. Bo Friends Blog: http://zhou123.blog.51cto.com/4355617/1312791

Python calls the three generous methods of shell commands

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.