将函数结果写入标准输入 [英] Write function result to stdin

查看:30
本文介绍了将函数结果写入标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将函数的结果写入标准输入.

I'm trying to write the results of a function to stdin.

这是代码:

def testy():
    return 'Testy !'

import sys
sys.stdin.write(testy())

我得到的错误是:

Traceback (most recent call last):
  File "stdin_test2.py", line 7, in <module>
    sys.stdin.write(testy())
io.UnsupportedOperation: not writable

我不完全确定,这是正确的做事方式吗?

I'm not completely sure, is this the right way of doing things ?

推荐答案

你可以用类似文件的对象模拟 stdin 吗?

You could mock stdin with a file-like object?

import sys
import StringIO

oldstdin = sys.stdin
sys.stdin = StringIO.StringIO('asdlkj')

print raw_input('.')       #  .asdlkj

这篇关于将函数结果写入标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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