subprocess.Popen() 常用方法
p.stdout.read() :用于读取标准输出,会一次性读取所有内容,返回一个字符串
p.stdout.readline() :用于读取标准输出,一次只读取一行内容,返回一个字符串
p.stdout.readlines() :用于读取标准输出,一次性读取所有内容,返回一个列表,每一行是列表的一个元素
from subprocess import Popen, PIPE p = Popen('ls /data', stdout=PIPE, shell=True)
for line in p.stdout.readlines():
print(line),
[root@localhost ~]$ python 1.py
1.txt
2.txt
3.txt
p.wait() :等待子进程结束,并返回状态码。如下,如果没有加上 p.wait(),则 sleep 100 还没有执行完,就会执行 print('End'),如果加上就会等待 sleep 100 执行完
from subprocess import Popen, PIPE p = Popen('sleep 100', stdout=PIPE, shell=True)
p.wait()
print('End')
p.pid :用于查看子进程的PID
from subprocess import Popen, PIPE p = Popen('sleep 100', stdout=PIPE, shell=True)
print(p.pid)
[root@localhost ~]$ python 1.py
35327
[root@localhost ~]$ ps aux | grep 35327
root 35327 0.0 0.0 107904 612 pts/0 S 17:56 0:00 sleep 100
root 35329 0.0 0.0 112676 984 pts/0 S+ 17:57 0:00 grep --color=auto 35327
p.poll() :用于检查子进程(命令)是否已经执行结束,没结束返回None,结束后返回状态码
#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
from subprocess import Popen, PIPE p = Popen('sleep 3', stdout=PIPE, shell=True) while True:
if p.poll() == None:
print('程序执行中...')
time.sleep(1)
else:
print('程序执行完毕, 状态码为:%s' % p.poll())
break
[root@localhost ~]$ python 1.py
程序执行中...
程序执行中...
程序执行中...
程序执行完毕, 状态码为:0
p.returncode :用于返回命令执行完之后的状态码
#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
from subprocess import Popen, PIPE p = Popen('sleep 3', stdout=PIPE, shell=True) while True:
if p.poll() == None:
print('程序执行中...')
time.sleep(1)
else:
print('程序执行完毕, 状态码为:%s' % p.returncode)
break
[root@localhost ~]$ python 1.py
程序执行中...
程序执行中...
程序执行中...
程序执行完毕, 状态码为:0
p.kill() :用于杀死子进程
from subprocess import Popen, PIPE p = Popen('sleep 100', stdout=PIPE, shell=True)
print(p.pid)
p.kill()
[root@localhost ~]$ python 1.py
35403
[root@localhost ~]$ ps aux | grep 35403 # 可以看到子进程已经不在了
root 35405 0.0 0.0 112676 980 pts/0 S+ 18:12 0:00 grep --color=auto 35403
p.terminate() :用于终止子进程,与 kill() 差不多
from subprocess import Popen, PIPE p = Popen('sleep 100', stdout=PIPE, shell=True)
print(p.pid)
p.terminate()
[root@localhost ~]$ python 1.py
35403
[root@localhost ~]$ ps aux | grep 35403 # 可以看到子进程已经不在了
root 35405 0.0 0.0 112676 980 pts/0 S+ 18:12 0:00 grep --color=auto 35403
p.communicate() :该方法可用来与子进程进行交互,比如发送数据到stdin,结果会返回一个元组,这个元组包含stdout和stderr
from subprocess import Popen, PIPE p = Popen('cat', stdin=PIPE, stdout=PIPE, shell=True)
print p.communicate('hello world')
[root@localhost ~]$ python 1.py
('hello world', None)
subprocess.Popen() 常用方法的更多相关文章
- Python subprocess.Popen communicate() 和wait()使用上的区别
之所以会纠结到这个问题上是因为发现在调用Popen的wait方法之后程序一直没有返回.google发现wait是有可能产生死锁的.为了把这个问题彻底弄清楚,搜索一些资料过来看看: 原文链接:http: ...
- python subprocess.Popen 非阻塞
1.非阻塞设置subprocess.Popen(args, stdout=subprocess.PIPE,stderr=subprocess.PIPE) def non_block_read(outp ...
- python中subprocess.Popen.poll
import subprocess proc = subprocess.Popen(['python', 'test.py'], stdout=subprocess.PIPE) while 1: pr ...
- Python subprocess Popen
目的:顺序执行进程 在Bash里面类似 a.sh && b.sh && c.sh 先来说下Popen这个函数 class subprocess.Popen(args ...
- python中的subprocess.Popen()使用
python中的subprocess.Popen()使用 从python2.4版本开始,可以用subprocess这个模块来产生子进程,并连接到子进程的标准输入/输出/错误中去,还可以得到子进程的返回 ...
- python subprocess.Popen 控制台输出 实时监控百度网ping值
import subprocess file_out = subprocess.Popen('ping www.baidu.com', shell=True, stdout=subprocess.PI ...
- Python调用subprocess.Popen卡死的解决方案
转载自:https://www.cnblogs.com/keke-xiaoxiami/p/7875009.html 在Python中,调用:subprocess.Popen(cmd, stdout = ...
- subprocess.Popen指令包含中文导致乱码问题解决
其实解决起来非常简单,如果了解到Windows中文系统编码为GB2312的话 只需将你包含中文的指令字符串编码为GB2312即可 cmd = u'cd 我的文档' cmd.encode('gb2312 ...
- python - 远程主机执行命令练习(socket UDP + subprocess.Popen()) 练习1
环境是windows 环境. server端: import socket import subprocess ss = socket.socket(socket.AF_INET,socket.SOC ...
随机推荐
- 请介绍WCF服务
WCF本质上提供一个跨进程.跨机器以致跨网络的服务调用 WCF合并了Web服务..net Remoting.消息队列和Enterprise Services的功能并集成在Visual Studio中, ...
- Memory leak patterns in JavaScript
Handling circular references in JavaScript applications Plugging memory leaks in JavaScript is easy ...
- Jackson 时间格式化,时间注解 @JsonFormat 与 @DatetimeFormat 用法、时差问题说明
@JsonFormat 使用 我们可以有两种用法(我知道的),在对象属性上,或者在属性的 getter 方法上,如下代码所示: 增加到属性上: ... ... /**更新时间 用户可以点击更新,保存最 ...
- swift 属性和方法
属性和常量 如果创建了一个结构体的实例并赋值给一个常量,则无法修改实例的任何属性: let rangeOfFourItems = FixedLengthRange(firstValue: 0, len ...
- 06、Windows 10 技术预览
随着 Windows 10 发布的,未来 Windows 平台都是统一开发模型,可以只写一个 Appx 包,就可以同时部署到 Windows/ Windowsw Phone/ Tablet /xbox ...
- dp之多重背包2191
水题........ #include<iostream> #include<stdio.h> #include<string.h> using namespace ...
- Win10系统修改电脑IP地址
方法/步骤 1.首先,打开控制面板 2.接着,点开“网络和Internet”,再点开“网络和共享中心” 3.点击"无线网络连接IT4822",可以看到下图 4.然后点击开“属性”, ...
- 示例 - 如何在Console应用程序中应用SpiderStudio生成的DLL?
以前面生成的XML/JSON互转DLL为例, 我们写一个Console Appliction来做这件事情, 步骤如下: 1. 创建Console Project 2. 引入www.utilities_ ...
- mysql实现经纬度计算两个坐标之间的距离
DELIMITER $$CREATE DEFINER = CURRENT_USER FUNCTION `getDistance`(`lon1` float,`lat1` float,`lon2` fl ...
- GDI+学习笔记
7.1.1 GDI+概述 GDI+是微软在Windows 2000以后操作系统中提供的新的图形设备接口,其通过一套部署为托管代码的类来展现, 这套类被称为GDI+的“托管类接口”,GDI+主要提供了以 ...