[英]How to read and check stdin/stdout in python?
对于如何读取标准输入,我们有这个很好的答案。
stdin
? print
是否像stdout
? 我问它因为我在facebook演示拼图中看到了这个订单:
注意:您需要编写完整代码,所有输入都来自stdin和输出到stdout如果您使用的是“Java”,则类名为“Solution”
当您从stdin
阅读时,您有三个基本选项:
手动输入东西:
$ python hello.py asfasfasf asfasfasfasfasf <TYPE CONTROL-D TO END STREAM>
使用<
:
$ python hello.py < inputfile.txt
使用上一个命令的输出:
$ cat inputfile.txt | grep oranges | python hello.py
所有这三个都会通过stdin
给你输入。
编辑完问题后,您不再提出同样的问题。 以下是您的新问题的答案:
sys.stdin = open('inputfile.txt')
以使输入文件看起来像stdin。 确保这是你想要的。 从你的作业提示,听起来我的上述解决方案是你想要的。 print
写入stdout。 如果要运行代码以便从stdin(而不是从交互式终端)读取文件 ,则使用重定向。
python program.py <input_file.txt
<
表示在运行脚本时,指定文件将附加到stdin。 这种语法在Windows,MacOS和类Unix平台上是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.