Python3 执行Linux Bash命令的方法
作者:nudt_qxx 时间:2023-11-03 06:47:15
和之前C++执行Linux Bash命令的方法 一样,Python依然支持system调用和popen()函数来执行linux bash命令。
方法一:system调用
#仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息
import os
os.system('ls')
方法二:popen()函数
import os
os.popen('ls').readlines() #这个返回值是一个list
方法三:使用模块 subprocess
import subprocess
subprocess.call('ls') #可以直接call()调用
'''
#也可以使用subprocess.Popen
p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print(line)
'''
来源:https://blog.csdn.net/xiangxianghehe/article/details/77407378
标签:Python3,Linux,Bash
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用ASP和SQL语句动态的创建Access表
2008-10-14 16:59:00
Python实现内存泄露排查的示例详解
2021-03-03 21:55:29
详解python日期时间处理
2021-08-20 17:07:53
![](https://img.aspxhome.com/file/2023/6/114596_0s.png)
关于团队建设以及网站建设的琐事
2009-03-19 13:31:00
python 实时得到cpu和内存的使用情况方法
2023-10-05 20:03:24
![](https://img.aspxhome.com/file/2023/1/86341_0s.jpg)
Python logging日志库空间不足问题解决
2021-08-24 15:59:17
![](https://img.aspxhome.com/file/2023/8/105348_0s.png)
精细讲述SQL Server数据库备份多种方法
2009-01-13 13:33:00
Python实现Youku视频批量下载功能
2022-08-14 21:16:45
![](https://img.aspxhome.com/file/2023/0/105440_0s.png)
golang 执行命令行的实现
2024-04-28 09:14:40
解决Windows10下mysql5.5数据库命令行中文乱码问题
2024-01-16 15:04:29
python中有关时间日期格式转换问题
2023-03-17 07:43:12
![](https://img.aspxhome.com/file/2023/8/83658_0s.png)
Python OpenCV图像颜色变换示例
2023-07-30 06:05:00
![](https://img.aspxhome.com/file/2023/5/93505_0s.jpg)
Oracle常用dump命令,记录一下备查。
2009-03-04 10:27:00
解析arp病毒背后利用的Javascript技术
2007-08-08 09:55:00
Python的垃圾回收机制详解
2023-06-03 16:03:24
如何列举Error的所有对象?
2010-01-12 20:01:00
基于Python实现简易的动漫图片转换器
2022-12-25 23:23:11
![](https://img.aspxhome.com/file/2023/4/117604_0s.png)
一个Access数据库数据传递的实例方法
2008-11-28 16:24:00
python实现AI聊天机器人详解流程
2022-12-11 23:57:37
![](https://img.aspxhome.com/file/2023/5/109575_0s.jpg)
基于Python实现微信聊天界面生成器
2021-12-19 20:36:02
![](https://img.aspxhome.com/file/2023/6/67906_0s.png)