原文标题:python执行linux命令的两种方法
python执行linux命令有两种方法:
在此以Linux常用的ls命令为例:
shell# python >> import os >> os.system('ls -l')
执行结果:
drwxr-xr-x 2 root root4096 2012-03-12 bin drwxr-xr-x 4 root root1024 2011-10-31 boot drwxr-xr-x 6 root root4096 2011-11-22 data drwxr-xr-x12 root root3620 01-11 16:01 dev drwxr-xr-x93 root root 12288 01-17 04:02 etc drwxr-xr-x16 root root4096 10-18 18:53 home drwxr-xr-x11 root root4096 2012-03-12 lib drwxr-xr-x 8 root root4096 2012-01-20 lib64 drwx------ 2 root root 16384 2011-10-31 lost+found drwxr-xr-x 2 root root4096 2010-01-27 media drwxr-xr-x 2 root root 0 01-11 16:00 misc drwxr-xr-x 2 root root4096 2011-12-02 mnt drwxr-xr-x 2 root root 0 01-11 16:00 net drwxr-xr-x12 root root4096 2011-11-22 new drwxr-xr-x 2 root root4096 2010-01-27 opt dr-xr-xr-x 168 root root 0 01-11 15:59 proc drwxr-x--- 6 root root4096 11-06 11:30 root drwxr-xr-x 2 root root 12288 2012-03-31 sbin drwxr-xr-x 2 root root4096 2011-10-31 selinux drwxr-xr-x 2 root root4096 2010-01-27 srv drwxr-xr-x11 root root 0 01-11 15:59 sys drwxrwxrwt 4 root root 20480 01-18 04:02 tmp drwxr-xr-x16 root root4096 07-25 16:34 usr drwxr-xr-x21 root root4096 2011-11-02 var
方法二:使用subprocess模块
shell# python >> import subprocess >> subprocess.call('ls -l'.split())
执行结果是相同的。
ps:开发中最常用的方法是os模块方法。
[版权声明]
本文标题:python执行linux命令的两种方法作者:python学习
本文链接:http://www.ccpit.org.cn/jichu/jc7086.html——此文章系本站原创/整理,转载请标明出处、原标题、链接