在google上搜了一下,发现在Python 里调用系统命令的方法有N种。
1. 使用os.popen
>>> import os
>>> pipe=os.popen("df -lh").read()
>>> print pipe
/dev/sda6 2.0G 333M 1.6G 18% /
/dev/sda1 99M 16M 79M 17% /boot
tmpfs 948M 0 948M 0% /dev/shm
/dev/sda7 115G 96G 14G 88% /home
/dev/sda2 7.8G 3.3G 4.2G 45% /usr
/dev/sda5 3.9G 302M 3.4G 9% /va
这种方式可以将返回结果赋给一个变量,可以在程序中处理这些结果。 这种方法比较方便。
2. 使用os.system()
>>> import os
>>> os.system('df -lh')
/dev/sda6 2.0G 333M 1.6G 18% /
/dev/sda1 99M 16M 79M 17% /boot
tmpfs 948M 0 948M 0% /dev/shm
/dev/sda7 115G 96G 14G 88% /home
/dev/sda2 7.8G 3.3G 4.2G 45% /usr
/dev/sda5 3.9G 302M 3.4G 9% /var
这个命令仅在终端执行命令。
3. 使用 subprocess.call()
>>> import subprocess
>>> subprocess.call(['df','-lh'])
/dev/sda6 2.0G 333M 1.6G 18% /
/dev/sda1 99M 16M 79M 17% /boot
tmpfs 948M 0 948M 0% /dev/shm
/dev/sda7 115G 96G 14G 88% /home
/dev/sda2 7.8G 3.3G 4.2G 45% /usr
/dev/sda5 3.9G 302M 3.4G 9% /var
0
>>>
subprocess.call(*popenargs, **kwargs)
Run command with arguments. Wait for command to complete, then return the returncode attribute.
4. 使用subprocess.Popen(command, shell=True)
帮助文档对该方法说明:
class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
Arguments are:
args should be a string, or a sequence of program arguments. The program to execute is normally the first item in the args sequence or the string if a string is given, but can be explicitly set by using the executable argument. When executable is given, the first item in the args sequence is still treated by most programs as the command name, which can then be different from the actual executable name. On Unix, it becomes the display name for the executing program in utilities such as ps.
On Unix, with shell=False (default): In this case, the Popen class uses os.execvp() to execute the child program. args should normally be a sequence. If a string is specified for args, it will be used as the name or path of the program to execute; this will only work if the program is being given no arguments.
>>> import subprocess
>>> cmd='df -lh'
>>> subprocess.Popen(cmd,shell=True)
<subprocess.Popen object at 0xb7eab26c>
/dev/sda6 2.0G 333M 1.6G 18% /
/dev/sda1 99M 16M 79M 17% /boot
tmpfs 948M 0 948M 0% /dev/shm
/dev/sda7 115G 96G 14G 88% /home
/dev/sda2 7.8G 3.3G 4.2G 45% /usr
/dev/sda5 3.9G 302M 3.4G 9% /var
关于这些命令的具体说明,可以参考联机文档。
------------------------------------------------------------------------------
Blog: http://blog.csdn.net/tianlesoftware
网上资源: http://tianlesoftware.download.csdn.net
相关视频:http://blog.csdn.net/tianlesoftware/archive/2009/11/27/4886500.aspx
DBA1 群:62697716(满); DBA2 群:62697977(满)
DBA3 群:62697850 DBA 超级群:63306533;
聊天 群:40132017
--加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com