日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python输出重定向到窗口_[python]重定向输出

發(fā)布時(shí)間:2024/6/1 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python输出重定向到窗口_[python]重定向输出 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文作者:riag

本文出處:http://blog.csdn.net/riag

聲明: 本文可以不經(jīng)作者同意, 任意復(fù)制, 轉(zhuǎn)載, 但任何對(duì)本文的引用都請(qǐng)保留文章開始前三行的作者, 出處以及聲明信息. 謝謝.

調(diào)用一個(gè)控制臺(tái)程序,獲取它的標(biāo)準(zhǔn)輸出,或把它的標(biāo)準(zhǔn)輸出重定向到界面上,這里只介紹如何獲取它的標(biāo)準(zhǔn)輸出,因?yàn)樵矶家粯拥摹?/p>

使用python2.5的subprocess模塊來(lái)實(shí)現(xiàn)。

import sys

import subprocess

def RunShellWithReturnCode(command, print_output=False,

universal_newlines=True):

"""Executes a command and returns the output from stdout and the return code.

Args:

command: Command to execute.

print_output: If True, the output is printed to stdout.

If False, both stdout and stderr are ignored.

universal_newlines: Use universal_newlines flag (default: True).

Returns:

Tuple (output, return code)

"""

p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,

shell=use_shell, universal_newlines=universal_newlines)

if print_output:

output_array = []

while True:

line = p.stdout.readline()

if not line:

break

print line.strip("/n")

output_array.append(line)

output = "".join(output_array)

else:

output = p.stdout.read()

p.wait()

errout = p.stderr.read()

#if print_output and errout:

# print >>sys.stderr, errout

p.stdout.close()

p.stderr.close()

return output, p.returncode

def RunShell(command, silent_ok=False, universal_newlines=True,

print_output=False):

data, retcode = RunShellWithReturnCode(command, print_output,

universal_newlines)

if retcode:

ErrorExit("Got error status from %s:/n%s" % (command, data))

if not silent_ok and not data:

ErrorExit("No output from %s" % command)

return data

上面的代碼來(lái)自于 開源的代碼審查工具 rietveld 的代碼,其中RunShellWithReturnCode函數(shù) 就是返回控制臺(tái)的標(biāo)準(zhǔn)輸出和返回結(jié)果

總結(jié)

以上是生活随笔為你收集整理的python输出重定向到窗口_[python]重定向输出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。