Python Glossary Русский

all Python 3fileinputfunction

fileinput.fileno()

reading state

Returns the integer file descriptor of the current fileinput file, or -1 when no file is open.

Syntax

fileinput.fileno() -> int

-1 is returned before the first line and between files — not an error but "no file", and it must not be passed to os.fstat(). The descriptor belongs to fileinput: do not close it yourself, it is closed when the next file starts.

Examples

import fileinput, os
open('a.txt', 'w', encoding='utf-8').write('a1\na2\n')
with fileinput.input(['a.txt'], encoding='utf-8') as f:
    print(fileinput.fileno())  # → -1
    next(f)
    fd = fileinput.fileno()
    print(fd >= 0, os.fstat(fd).st_size)  # → True 6

See also

Open in the full glossarydocs.python.orgReport an inaccuracy