Preface: Busy with recent tasks, you need to use some LIBSVM commands, such as running Java svm_train train_file model_file at the terminal. Pythonsubset.py file train_num train_file test_file commands, but file preparation is also written by Python, file needs to be LIBSVM acceptable format, So use Python to write good features, to LIBSVM acceptable format, generate file, and then call training at the terminal. The command to run the terminal directly inside the Python code. Bo Friends Blog Description is very detailed, here directly reproduced and make some comments.
#=====================================================
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 tips can be seen there are 8, help (OS.EXECL) and so can find its usage description.
System method for 1.2.os modules
The system method creates a child process that runs an external program that returns only the results of the external program's operation. 0 indicates a successful run.
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]:
as above, some basic shell commands, passed into the Os.system () parameter, can be executed. However, the return value of the command cannot be obtained.
1.3.os Module
popen method
The Popen method can get the return value of the shell command, and after Os.popen (CMD), it needs 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~
Note that after read () or ReadLines (), each of its elements contains a carriage return \ n.
Second,
commands module
Using the GetOutput method of the commands module, this method differs from Popend in that Popen returns a file handle, and this method returns the output of the external program as a string, which in many cases is more convenient to use.
Main methods:
* Commands.getstatusoutput (CMD) return (status, output)
* Commands.getoutput (CMD) returns only output results
* Commands.getstatus (file) returns the execution result string for Ls-ld file, called GetOutput, and is not recommended for use with 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
Using the Subprocess module, you can create new processes that can connect to the input/output/error pipelines of the new process and get the return status of the new process execution. 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)If command is not an executable file, Shell=true cannot be saved.
Using the Subprocess module, you can create new processes that can connect to the input/output/error pipelines of the new process and get the return status of the new process execution. 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 the child process.
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. If command is not an executable file, Shell=true is not allowed to be omitted. Shell=true means the command is executed under the shell.
#======================== the following reprint ============
4. Comparison and summary of the public methods4.1. About Os.systemOs.system ("Some_command with args") passes commands and parameters to your system shell, which is good because you can run multiple commands at the same time in this way and you can set up pipelines and input and output redirects . Like what:
Os.system ("Some_command < Input_file | Another_command > Output_file ")
However, while this is convenient, you need to handle the escape of shell characters manually, such as spaces. In addition, this will only allow you to run simple shell commands and not run 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 everything through a string, your commands are passed to the shell; If you pass them through a list, you don't have to worry about running away from anything.
4.3. About Subprocess.popenThe Popen class of the subprocess module, intended as an alternative to Os.popen, but because it is very comprehensive so than os.popen to appear slightly more complex , use up need 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 (). However, it is good to use a unified class that includes 4 different popen functions.
4.4. About Subprocess.callthe 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 gives you a return code. For example:
Return_code = Subprocess.call ("Echo Hello World", Shell=true)
#==========================================
Reference:
1. Bo Friends Blog: http://blog.csdn.net/longerzone/article/details/17889969
2. Bo Friends Blog: http://zhou123.blog.51cto.com/4355617/1312791
Three ways Python invokes the shell command