重定向os.system输出 [英] Redirect os.system output

查看:185
本文介绍了重定向os.system输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将os.system的输出重定向到变量,但

遇到问题。我尝试使用os.popen(..)。read()...但是那个

并没有给我我想要的东西。


......顺便说一句,这是Windows。


例如:

tmp = os.popen(" hostname")。read()


....按预期工作。


但是,


tmp = os.popen( " cmd")。read()

....我想访问cmd进程...即输入命令

就像一个普通的命令行。 os.system()允许这个,但我不想把

输出到屏幕上。我想把它存储到变量中。然后在其他地方发送

变量内容,接收更多输入并提交。

几乎模仿windows命令提示符。


any想法?

I would like to redirect the output from os.system to a variable, but
am having trouble. I tried using os.popen(..).read() ...but that
doesn''t give me exactly what i want.

...this is windows by the way.

For example:
tmp = os.popen("hostname").read()

....works as expected.

however,

tmp = os.popen("cmd").read()
....i would like to have access to the cmd process...i.e. enter commands
like a normal command line. os.system() allows this, but i dont want
output to the screen..i wanna store it to a variable. then send
content of variable elsewhere, receive more input and submit it.
almost emulate the windows command prompt.

any ideas?

推荐答案

也许你应该看看子流程模块


我有一个expamle ,这是Linux虽然
maybe you should look at subprocess module

I have one expamle, this is Linux though
import subprocess as sp
p1 = sp.Popen([" ls"," -l"],stdout = sp.PIPE)
p2 = sp.Popen([" wc"," -c"],stdin = p1.stdout,stdout = sp.PIPE )
打印p2.stdout.read()
import subprocess as sp
p1 = sp.Popen(["ls", "-l"], stdout = sp.PIPE)
p2 = sp.Popen(["wc", "-c"], stdin = p1.stdout, stdout = sp.PIPE)
print p2.stdout.read()



226

hth,Daniel


226

hth, Daniel


jas写道:
我想将输出从os.system重定向到变量,但是
am遇到麻烦。我尝试使用os.popen(..)。read()...但是那个
并没有完全给我我想要的东西。


以下是使用子流程的示例:
http://groups.google.com/group/comp....7e8e2a3?hl=en&

Kent

..顺便提一下这是窗户。

例如:
tmp = os.popen(" hostname")。read()

> ...按预期工作。

然而,

tmp = os.popen(" cmd")。read()
...我会喜欢访问cmd进程...即输入命令
就像一个普通的命令行。 os.system()允许这个,但我不想输出到屏幕..我想将它存储到一个变量。然后在其他地方发送变量内容,接收更多输入并提交它。
几乎模仿windows命令提示符。

任何想法?
I would like to redirect the output from os.system to a variable, but
am having trouble. I tried using os.popen(..).read() ...but that
doesn''t give me exactly what i want.
Here is an example using subprocess:
http://groups.google.com/group/comp....7e8e2a3?hl=en&

Kent

..this is windows by the way.

For example:
tmp = os.popen("hostname").read()

...works as expected.

however,

tmp = os.popen("cmd").read()
...i would like to have access to the cmd process...i.e. enter commands
like a normal command line. os.system() allows this, but i dont want
output to the screen..i wanna store it to a variable. then send
content of variable elsewhere, receive more input and submit it.
almost emulate the windows command prompt.

any ideas?



还有其他想法吗?或使用子进程做我的事情的例子

要求?

Kent Johnson写道:
Any other ideas? or examples of using subprocess to do what I was
asking?
Kent Johnson wrote:
jas写道:
我想将os.system的输出重定向到变量,但是
遇到了麻烦。我尝试使用os.popen(..)。read()......但是
并没有给我我想要的东西。
I would like to redirect the output from os.system to a variable, but
am having trouble. I tried using os.popen(..).read() ...but that
doesn''t give me exactly what i want.



这是一个例子使用子流程:
http://groups.google.com/group/comp....7e8e2a3?hl=en&

Kent



Here is an example using subprocess:
http://groups.google.com/group/comp....7e8e2a3?hl=en&

Kent


..顺便说一下这是Windows。

例如:
tmp = os.popen(" hostname")。read()

...按预期工作。

然而,

tmp = os.popen(" cmd")。read()
。 ..i想要访问cmd进程...即输入命令
就像一个普通的命令行。 os.system()允许这个,但我不想输出到屏幕..我想将它存储到一个变量。然后在其他地方发送变量内容,接收更多输入并提交它。
几乎模仿windows命令提示符。

任何想法?

..this is windows by the way.

For example:
tmp = os.popen("hostname").read()

...works as expected.

however,

tmp = os.popen("cmd").read()
...i would like to have access to the cmd process...i.e. enter commands
like a normal command line. os.system() allows this, but i dont want
output to the screen..i wanna store it to a variable. then send
content of variable elsewhere, receive more input and submit it.
almost emulate the windows command prompt.

any ideas?






这篇关于重定向os.system输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆