當前位置:首頁 » 編程語言 » 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

熱點內容
python做腳本 發布:2025-02-11 17:05:42 瀏覽:548
風神瞳腳本 發布:2025-02-11 17:02:18 瀏覽:690
物理化學壓縮 發布:2025-02-11 17:02:03 瀏覽:295
蔚來配置哪些值得加 發布:2025-02-11 16:58:28 瀏覽:325
索引型資料庫 發布:2025-02-11 16:58:26 瀏覽:916
hbasephp 發布:2025-02-11 16:44:41 瀏覽:761
微軟不給源碼 發布:2025-02-11 16:13:37 瀏覽:38
php的get方法 發布:2025-02-11 16:12:30 瀏覽:967
源碼網嘉 發布:2025-02-11 16:07:06 瀏覽:192
免費ftp服務軟體 發布:2025-02-11 15:58:06 瀏覽:866