MyTetra Share
Делитесь знаниями!
fileno()
Время создания: 31.08.2017 21:01
Текстовые метки: knowledge
Раздел: Python - Потоки
Запись: xintrea/mytetra_db_mcold/master/base/150349326939tyo0fm7w/text.html на raw.githubusercontent.com


favorite

2

I am sorry if it's very basic or already asked before (I googled but couldn't find a simple & satisfactory explanation).

I want to know what sys.stdin.fileno() is?

I saw it in a code and didn't understand what it does. Here's the actual code block,

fileno = sys.stdin.fileno()

if fileno is not None:

new_stdin = os.fdopen(os.dup(fileno))

I just executed print sys.stdin.fileno() in my python command line and it returned 0.

I also searched google, and this (nullage.com)  is the reference I could find, but it also only says,

fileno() -> integer "file descriptor".

This is needed for lower-level file interfaces, such os.read().

So, what exactly does it mean?

python  file-access

s hareimprove this question

asked Aug 25 '15 at 8:50

Codebender

9,54831960

  

 

That means that it's needed for lower-level file interfaces (such as os.read). It's the low level identifier for the file (and it may not be applicable on all platforms), traditionally standard-in has descriptor 0, standard-out 1 and standard-err 2 and other files has descriptors >2. os.dup duplicates a descriptor (creates a low-level identifier for a copy of the file), and fdopen creates a high-level object from a low-level descriptor. – skyking Aug 25 '15 at 8:57  

add a comment

3 Answers

a ctiveoldest votes

Не нашли ответ? З адайте вопрос на Stack Overflow на русском.


up vote6down voteaccepted

File descriptor is a low-level concept, it's an integer that represents an open file. Each open file is given a unique file descriptor.

In Unix, by convention, the three file descriptors 01, and 2 represent standard input, standard output, and standard error, respectively.

>>> import sys

>>> sys.stdin.fileno()

0

>>> sys.stdout.fileno()

1

>>> sys.stderr.fileno()

2

 
MyTetra Share v.0.65
Яндекс индекс цитирования