任何人都可以在Python 3中给我一个stdin和stdout的快速教程吗?

Mar*_*lén 12 python stdin stdout python-3.x

我知道这听起来像我可以谷歌,但事实是,我没有找到或不理解很少的Python 3来源解释.

所以这是我的问题:

  • input()stdin在Python 3的功能?这是否意味着当你打开你的filename.py程序时,stdin用户输入的是什么?
  • 是Python 3中print()stdout函数,还是必须写入文件?
  • 对于Spotify难题,是说"输入是从标准输入读取".我应该我的文件中包含的stdinstdout

更新:这是否意味着我可以使用:

import sys
unfmtdDate = str(sys.stdin.read())
Run Code Online (Sandbox Code Playgroud)

...代替...

unfmtdDate = str(input())
Run Code Online (Sandbox Code Playgroud)

Woo*_*ble 11

stdin并且stdout是OS提供的类文件对象.通常,当程序在交互式会话中运行时,stdin是键盘输入并且stdout是用户的tty,但是shell可以用于将它们从普通文件或管道输出重定向到其他程序并输入到其他程序.

input()用于提示用户输入类型.在类似编程谜题的情况下,通常假设stdin从数据文件重定向,并且当给出输入格式时,通常最好使用sys.stdin.read()而不是提示输入input(). input()用于交互式用户输入,它可以显示提示(在sys.stdout上)并使用GNU readline库(如果存在)来允许行编辑等.

print()确实是最常用的写作方式stdout.没有必要做任何特殊的事情来指定输出流. 如果没有给它作为参数的备用文件,则print()写入.sys.stdoutfile=


And*_*ark 5

When you run your Python program, sys.stdin is the file object connected to standard input (STDIN), sys.stdout is the file object for standard output (STDOUT), and sys.stderr is the file object for standard error (STDERR).

Anywhere in the documentation you see references to standard input, standard output, or standard error, it is referring to these file handles. You can access them directly (sys.stdout.write(...), sys.stdin.read() etc.) or use convenience functions that use these streams, like input() and print().

For the Spotify puzzle, the easiest way to read the input would be something like this:

import sys
data = sys.stdin.read()
Run Code Online (Sandbox Code Playgroud)

After these two lines the input for your program is now in the str data.