如何获取os.system执行的脚本的stdout [英] how to get the stdout of the script that os.system executes

查看:170
本文介绍了如何获取os.system执行的脚本的stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在python中,os.system(cmd)可用于执行脚本或命令,如果成功则返回值为0,而不是它执行的脚本的标准输出。


例如


>>> a = os.system(" echo hello")

>>> a

0(而不是hello)


我的问题是如何将标准输出定向到我的python脚本中的变量。

i know that in python, os.system(cmd) can be used to execute a script or command, and the return value is 0 if successful, rather than the standard output of the script that it executes.

e.g.

>>> a=os.system("echo hello")
>>> a
0 (rather than "hello")

my question is how to direct the standard output into a variable in my python script.

推荐答案

我能从os.system获得输出的唯一方法是使用>在命令中使子shell将其输出写入文件。然后我所要做的就是读取文件。
The only way I''ve been able to get output from os.system is to use ">" in the command to make the subshell write its output to a file. Then all I have to do is read the file.
展开 | 选择 | Wrap | 行号



如果有一个更直接的方法会很好,所以希望其他人知道一个。我很想知道它是什么。
It would be nice if there was a more direct method, so hopefully someone else knows one. I''d be very interested to know what it is.



Tere是命令模块。


[code = python]

>> ;>导入命令

>>> print commands.getoutput(" echo helllo")

helllo

>>>导入命令

>>> print commands.getoutput(" echo hello")

hello

>>> txt = commands.getoutput(" uname -a")

>>> print txt

Linux Bob 2.6.20-15-server#2 SMP Sun Apr 15 07:41:34 UTC 2007 i686 GNU / Linux

[code]


我也认为os.popen有效。但我对此没有经验。命令快速简便。我认为os.popen更高级

Tere is the commands module.

[code=python]
>>> import commands
>>> print commands.getoutput("echo helllo")
helllo
>>> import commands
>>> print commands.getoutput("echo hello")
hello
>>> txt = commands.getoutput("uname -a")
>>> print txt
Linux Bob 2.6.20-15-server #2 SMP Sun Apr 15 07:41:34 UTC 2007 i686 GNU/Linux
[code]

I also think os.popen works. but i have no experience with that. commands is quick and easy. i think os.popen is more advanced



i知道在python中,os.system(cmd)可用于执行脚本或命令,如果成功则返回值为0,而不是它执行的脚本的标准输出。


例如


>>> a = os.system(" echo hello")

>>> a

0(而不是你好)


我的问题是如何将标准输出定向到我的python脚本中的变量。
i know that in python, os.system(cmd) can be used to execute a script or command, and the return value is 0 if successful, rather than the standard output of the script that it executes.

e.g.

>>> a=os.system("echo hello")
>>> a
0 (rather than "hello")

my question is how to direct the standard output into a variable in my python script.



由于你没有说明你的操作系统,我们的朋友史密斯已经给出了一个很好的解决方案(如果你在* nix下运行)。但是,我建议您使用较新的子进程模块。

Since you have not stated your OS, our freind Smygis has given a great solution (if you are running under *nix). I''d recommend, however, that you use the newer subprocess module.

6.8子流程 - 子流程管理


版本2.4中的新功能。


子进程模块允许您生成新进程,连接到它们的输入/输出/错误管道,并获取它们的返回代码。该模块旨在替换其他几个较旧的模块和功能,例如:


os.system

os.spawn *

os.popen *

popen2。*

命令。*
6.8 subprocess -- Subprocess management

New in version 2.4.

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several other, older modules and functions, such as:


os.system
os.spawn*
os.popen*
popen2.*
commands.*


这篇关于如何获取os.system执行的脚本的stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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