python调用shell命令之三慷慨法
preface: 忙于近期的任务,须要用到libsvm的一些命令。如在终端执行java svm_train train_file model_file. pythonsubset.py file train_num train_file test_file等命令。但file的准备又是通过python写好的。file须要是libsvm可以接受的格式。故用python写好特征。转为libsvm可以接受的格式。生成file,然后在终端调用训练。故想着直接在python代码里面直接执行终端的命令。博友博客描写叙述得非常具体,这里直接转载过来并做些凝视了。
#=====================================================
一、os 模块
1.1.os模块的exec方法簇:
ipython交互界面中:
In [1]: import os
In [2]: os.exec
os.execl os.execlp os.execv os.execvp
os.execle os.execlpe os.execve os.execvpe
In [2]: os.execl?
Type: function
String form: <function execl at 0xb73673e4>
File: /home/shifeng/anaconda/lib/python2.7/os.py
Definition: os.execl(file, *args)
Docstring:
execl(file, *args)
Execute the executable file with argument list args, replacing the
current process.
os.exec+Tab键智能提示能够看到有8个,help(os.execl)等能够找到其使用方法说明。
1.2.os模块的system方法
system方法会创建子进程执行外部程序。方法仅仅返回外部程序的执行结果。0表示执行成功。
In [10]: os.system("echo \"hello world\"")如上。一些主要的shell命令,传入os.system()參数里面,便能运行。
hello world
Out[10]: 0
In [11]: 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]: 0
In [12]: os.system("cat test.sh")
echo "hello world"
Out[12]: 0
In [13]: os.system("sh test.sh")
hello world
Out[13]: 0
只是无法得到命令的返回值。
1.3.os模块popen方法
popen方法可以得到shell命令的返回值。os.popen(cmd)后,须要再调用read()或者readlines()这两个命令。输出结果。
In [14]: os.popen("ls")
Out[14]: <open file 'ls', mode 'r' at 0xb67efd30>
In [15]: 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 [16]: 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 [17]: s = os.popen("ls").read()
In [18]: for i in s.split("\n"):
....: print i
....:
all_roc_plot.py~
scrapy_work
task1_feature_all2.py
test
test.py
test.py~
test.sh
test.sh~
ui_without_buy~
注意。read()或者readlines()后,其每一个元素包括一个回车符\n。
二、commands模块
使用commands模块的getoutput方法,这样的方法同popend的差别在于popen返回的是一个文件句柄,而本方法将外部程序的输出结果当作字符串返回。非常多情况下用起来要更方便些。
主要方法:
* commands.getoutput(cmd) 仅仅返回输出结果
* commands.getstatus(file) 返回ls -ld file的运行结果字符串,调用了getoutput。不建议使用此方法
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 [10]: 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~')
三、subprocess模块
使用subprocess模块能够创建新的进程。能够与新建进程的输入/输出/错误管道连通。并能够获得新建进程运行的返回状态。使用subprocess模块的目的是替代os.system()、os.popen*()、commands.*等旧的函数或模块。
3.1.subprocess.call(["some_command","some_argument","another_argument_or_path"])
subprocess.call(command,shell=True)
3.2.subprocess.Popen(command,shell=True)更多相关文章
- python 带正则的search 模块
- python中查看变量内存地址的方法
- RHEL6误安装RHEL7的包导致glibc被升级后系统崩溃处理方法
- Linux - 模块编程初试
- Linux selinux关闭方法和防火墙关闭方法
- 让 Linux 启动时加载自己的驱动模块 .ko
- Linux系统下Tar文件安装方法
- linux shell脚本编程笔记(四): 获取字符串长度的七种方法
- linux 的终端字体色和背景色的修改方法(三)