sys.stdin读什么? [英] What does sys.stdin read?

查看:102
本文介绍了sys.stdin读什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解了如何打开文件,然后对它们使用Python的内置函数.但是sys.stdin如何工作?

for something in sys.stdin:
    some stuff here

lines = sys.stdin.readlines()

上面sys.stdin的两种不同用法之间有什么区别?它从哪里读取信息?是通过键盘还是我们仍然需要提供文件?

解决方案

因此,您使用了Python的内置函数",大概是这样的:

file_object = open('filename')
for something in file_object:
    some stuff here

这通过在文件对象上调用 iterator 来读取文件,该对象恰巧从文件中返回下一行.

您可以改用:

file_object = open('filename')
lines = file_object.readlines()

将从当前文件位置的行读取到列表中.

现在,sys.stdin只是另一个文件对象,恰好在程序启动之前由Python打开.您可以根据自己的意愿来处理该文件对象,但这实际上与其他任何文件对象都没有什么不同,只是您不需要open.

for something in sys.stdin:
    some stuff here

将遍历标准输入,直到到达文件末尾.这样:

lines = sys.stdin.readlines()

您的第一个问题实际上是关于使用文件对象的不同方式的.

第二,它从哪里读取?它正在从文件描述符0(零)读取.在Windows上,它是文件句柄0(零).默认情况下,文件描述符/句柄0连接到控制台或tty,因此实际上是从键盘读取的.但是,通常可以由外壳程序(如bash或cmd.exe)使用以下语法重定向:

myprog.py < input_file.txt 

这会将文件描述符零更改为读取文件而不是键盘.在UNIX或Linux上,它使用基础调用dup2().阅读您的shell文档以获取有关重定向的更多信息(如果您很勇敢,也可以man dup2).

I get how to open files, and then use Python's pre built in functions with them. But how does sys.stdin work?

for something in sys.stdin:
    some stuff here

lines = sys.stdin.readlines()

What's the difference between the above two different uses on sys.stdin? Where is it reading the information from? Is it via keyboard, or do we still have to provide a file?

解决方案

So you have used Python's "pre built in functions", presumably like this:

file_object = open('filename')
for something in file_object:
    some stuff here

This reads the file by invoking an iterator on the file object which happens to return the next line from the file.

You could instead use:

file_object = open('filename')
lines = file_object.readlines()

which reads the lines from the current file position into a list.

Now, sys.stdin is just another file object, which happens to be opened by Python before your program starts. What you do with that file object is up to you, but it is not really any different to any other file object, its just that you don't need an open.

for something in sys.stdin:
    some stuff here

will iterate through standard input until end-of-file is reached. And so will this:

lines = sys.stdin.readlines()

Your first question is really about different ways of using a file object.

Second, where is it reading from? It is reading from file descriptor 0 (zero). On Windows it is file handle 0 (zero). File descriptor/handle 0 is connected to the console or tty by default, so in effect it is reading from the keyboard. However it can be redirected, often by a shell (like bash or cmd.exe) using syntax like this:

myprog.py < input_file.txt 

That alters file descriptor zero to read a file instead of the keyboard. On UNIX or Linux this uses the underlying call dup2(). Read your shell documentation for more information about redirection (or maybe man dup2 if you are brave).

这篇关于sys.stdin读什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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