好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解在Python中执行系统命令的方法

最近在做那个测试框架的时候发现对python执行系统命令不太熟悉,所以想着总结下,下面这篇文章主要给大家介绍了关于在Python中执行系统命令的方法,需要的朋友可以参考借鉴,下面来一起看看吧。

>>> import os 
>>> output = os.system('cat /proc/cpuinfo') 
processor : 0 
vendor_id : AuthenticAMD 
cpu family : 21 
... ... 
>>> output # doesn't capture output 
0 
>>> output = os.popen('cat /proc/cpuinfo') 
>>> output 
<open file 'cat /proc/cpuinfo', mode 'r' at 0x7ff52d831540> 
>>> print output.read() 
processor : 0 
vendor_id : AuthenticAMD 
cpu family : 21 
... ... 
>>><span style="font-size:14px;"> 
>>> import commands 
>>> (status, output) = commands.getstatusoutput('cat /proc/cpuinfo') 
>>> print output 
processor : 0 
vendor_id : AuthenticAMD 
cpu family : 21 
... ... 
>>> print status 
0 
>>> import subprocess 
>>> subprocess.Popen(["ls", "-l"]) <strong> # python2.x</strong> doesn't capture output 
>>> subprocess.run(["ls", "-l"])  <strong># python3.x</strong> doesn't capture output 
<subprocess.Popen object at 0x7ff52d7ee490> 
>>> total 68 
drwxrwxr-x 3 xl xl 4096 Feb 8 05:00 com 
drwxr-xr-x 2 xl xl 4096 Jan 21 02:58 Desktop 
drwxr-xr-x 2 xl xl 4096 Jan 21 02:58 Documents 
drwxr-xr-x 2 xl xl 4096 Jan 21 07:44 Downloads 
... ... 
>>> 

以上就是详解在Python中执行系统命令的方法的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于详解在Python中执行系统命令的方法的详细内容...

  阅读:33次