欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python调用系统命令的方法

程序员文章站 2022-06-21 16:30:24
...

1、os.system

>>> import os
>>> os.system("ls /tmp")
'Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>'   aliyun_assist_37d3b524ccb1e5a4d7b7e65c04d5711b.zip   systemd-private-e409eeddae5a487c98ba5e177d72b3dc-chronyd.service-aphktJ
 aliyun_assist_37d3b524ccb1e5a4d7b7e65c04d5711b        AliyunAssistClientSingleLock.lock
0
>>> 
[3]+  Stopped                 python
[[email protected] bin]# ls /tmp
'Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>'   aliyun_assist_37d3b524ccb1e5a4d7b7e65c04d5711b.zip   systemd-private-e409eeddae5a487c98ba5e177d72b3dc-chronyd.service-aphktJ
 aliyun_assist_37d3b524ccb1e5a4d7b7e65c04d5711b        AliyunAssistClientSingleLock.lock
[[email protected] bin]# 

2、os.popen  

>>> import os
>>> tmp = os.popen("ls /tmp").readlines()
>>> print(tmp)
['Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>\n', 'aliyun_assist_37d3b524ccb1e5a4d7b7e65c04d5711b\n', 'aliyun_assist_37d3b524ccb1e5a4d7b7e65c04d5711b.zip\n', 'AliyunAssistClientSingleLock.lock\n', 'systemd-private-e409eeddae5a487c98ba5e177d72b3dc-chronyd.service-aphktJ\n']

3、subprocess

4、commands模块