当前位置:首页 » 编程语言 » python的popen

python的popen

发布时间: 2022-05-31 09:11:44

A. python popen查看命令有没有成功执行

通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是无法读取程序执行的返回值)尝试第三种方案 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。

B. python中的popen如何用c++实现

先调用pipe,再调用fork,把子进程的标准输入和输出调用p,到pipe的两个端,
通过pipe读取子进程的输出,并且可以:
通过pipe给子进程输入(可选)
调用wait等待子进程结束。

大体就这个意思

C. 如何操作 python os.popen的返回

ret = os.popen("ls").read()
但是,一些命令是不会输出消息的,所以调用read的时候会阻塞,你需要注意一下

D. python的subprocess.Popen()执行adb命令,adb报错返回中文数据时会出错,应该怎么办

你打包成exe后,命令行应该是pyinstller -Fw xxx.py
你加上了w参数也就是把console设置成了flase;那么os.popen()或者subprocess.popen()执行的时候没有载体,你只有把console设置成true,也就是命令改为pyinstaller -F xxx.py,这样你的os.popen()可执行,也能获得返回值。
的话还蛮多的但是非要说哪一个好玩的话就没有什么标准

E. python,os.popen 打包后出现问题

你打包成exe后,命令行应该是pyinstller -Fw xxx.py
你加上了w参数也就是把console设置成了flase;那么os.popen()或者subprocess.popen()执行的时候没有载体,你只有把console设置成true,也就是命令改为pyinstaller -F xxx.py,这样你的os.popen()可执行,也能获得返回值。

F. python popen怎么获取输出

# -*- coding:utf-8 -*-

import os
a = os.popen('hostname')
print a.read()

G. Python Popen communicate 和wait使用上的区别

简单说就是,使用 subprocess 模块的 Popen 调用外部程序,如果
stdout 或 stderr 参数是 pipe,并且程序输出超过操作系统的 pipe size时,如果使用
Popen.wait() 方式等待程序结束获取返回值,会导致死锁,程序卡在
wait() 调用上。
ulimit -a 看到的 pipe size 是 4KB,那只是每页的大小,查询得知 linux 默认的
pipe size 是 64KB。

看例子:
#!/usr/bin/env python
# coding: utf-8
# yc@2013/04/28

import subprocess

def test(size):
print 'start'

cmd = 'dd if=/dev/urandom bs=1 count=%d 2>/dev/null' % size
p = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
#p.communicate()
p.wait()

print 'end'

# 64KB
test(64 * 1024)

# 64KB + 1B
test(64 * 1024 + 1)

首先测试输出为 64KB 大小的情况。使用 dd 产生了正好 64KB 的标准输出,由 subprocess.Popen 调用,然后使用
wait() 等待 dd 调用结束。可以看到正确的 start 和 end 输出;然后测试比 64KB 多的情况,这种情况下只输出了
start,也就是说程序执行卡在了 p.wait() 上,程序死锁。具体输出如下:
start
end
start

那死锁问题如何避免呢?官方文档里推荐使用 Popen.communicate()。这个方法会把输出放在内存,而不是管道里,所以这时候上限就和内存大小有关了,一般不会有问题。而且如果要获得程序返回值,可以在调用
Popen.communicate() 之后取 Popen.returncode 的值。

结论:如果使用 subprocess.Popen,就不使用 Popen.wait(),而使用
Popen.communicate() 来等待外部程序执行结束。

Popen.wait()¶

Wait for child process to terminate. Set and returnreturncode
attribute.

Warning

This will deadlock when using stdout=PIPE and/orstderr=PIPE and the child process generates enough output to a pipe such that
it blocks waiting for the OS pipe buffer to accept more data. Use
communicate() to avoid that.

Popen.communicate(input=None)¶

Interact with process: Send data to stdin. Read data from stdout and
stderr, until end-of-file is reached. Wait for process to terminate.
The optionalinput argument should be a string to be sent to the child process, orNone,
if no data should be sent to the child.

communicate()
returns a tuple (stdoutdata,
stderrdata).

Note that if you want to send data to the process’s stdin, you need to create the Popen object with
stdin=PIPE. Similarly, to get anything other thanNone in the result tuple, you need to give
stdout=PIPE and/orstderr=PIPE too.

Note

The data read is buffered in memory, so do not use this method if the data size is large or unlimited.

subprocess 的两种方法:

1)如果想调用之后直接阻塞到子程序调用结束:

Depending on how you want to work your script you have two options.
If you want the commands to block and not do anything while it is
executing, you can just use
subprocess.call.
#start and block until done
subprocess.call([data["om_points"], ">", diz['d']+"/points.xml"])

2)非阻塞的时候方式:

If you want to do things while it is executing or feed things into stdin, you can use
communicate after the popen call.
#start and process things, then wait
p = subprocess.Popen(([data["om_points"], ">", diz['d']+"/points.xml"])
print "Happens while running"
p.communicate() #now wait

As stated in the documentation, wait can deadlock, so communicate is advisable.

H. python os.popen 出错

首先看下popen方法

报错没有找到"abd"这个文件。那么在python3里边是不是mode没有默认值"r",我用是python2。

解决方案:你可以试试 os.popen("abd","r")

I. Python fopen,open,和popen的区别

open标准posix接口,通用接口,不带缓冲区,效率和速度不错,用close关闭。
fopen基于posix进行封装,在glibc中,有缓冲区,效率也不错,主要多文件进行操作,用fclose关闭。
如:fopen(“/proc/partitions”, “r”));
popen为创建管道,内部fork进程执行shell命令,效率堪忧,用pclose关闭。
如:popen(“cat /proc/partitions”, “r”));

J. python的问题 subprocess.Popen

自从工作了就好久没发博客,还是出来冒个泡=。=
前段时间写的一个项目需要用python的subprocess.Popen大量调用某shell命令,运行到一定量级之后就会产生内存溢出,造成大量线程阻塞,然后就会造成([Errno 24] Too many open files)这个异常。
网上有人说是close_fds=True这个参数在python2.x默认没打开,这个参数可以关闭文件描述符,试了没有作用。
后来在国外某个人的帖子找到了和我类似的问题,解决办法就是执行后把stdin,stdout,stderr3个流进行清空即可。
结合网上的资料,写了一个可以自定义超时时间调用subprocess.Popen执行shell命令的函数(自定义超时为了避免某些shell卡死的情况),用这个函数去调用subprocess.Popen就不会产生上面这些问题了。
def timeout_command(command, timeout):
start = datetime.datetime.now()
process = subprocess.Popen(command, bufsize=10000, stdout=subprocess.PIPE, close_fds=True) while process.poll() is None:
time.sleep(0.1)
now = datetime.datetime.now() if (now - start).seconds> timeout: try:
process.terminate() except Exception,e: return None
return None
out = process.communicate()[0] if process.stdin:
process.stdin.close() if process.stdout:
process.stdout.close() if process.stderr:
process.stderr.close() try:
process.kill() except OSError: pass
return out

热点内容
光遇安卓怎么能加到ios 发布:2025-02-11 10:20:16 浏览:690
优势存储 发布:2025-02-11 10:20:14 浏览:362
光猫wifi怎么改密码 发布:2025-02-11 10:17:51 浏览:167
web和服务器怎么写通讯 发布:2025-02-11 10:08:06 浏览:979
安卓升级后手机变卡怎么办 发布:2025-02-11 09:58:01 浏览:113
土工数据库 发布:2025-02-11 09:48:55 浏览:963
libxml2编译 发布:2025-02-11 09:48:45 浏览:745
java类的复制 发布:2025-02-11 09:48:45 浏览:601
127小时ftp 发布:2025-02-11 09:47:10 浏览:852
安卓怎么看苹果手机的行驶轨迹 发布:2025-02-11 09:26:19 浏览:885