Python Glossary Русский

all Python 3osfunction

os.dup

os — file descriptors

Duplicate a file descriptor, returning a new one.

Syntax

os.dup(fd)

The new descriptor shares the file position and flags with the original: lseek on one moves the other. Since Python 3.4 the copy is non-inheritable by default. A typical use is saving a copy of stdout before redirecting it, so dup2 can restore it later.

Examples

import os
fd = os.open(os.__file__, os.O_RDONLY)
fd2 = os.dup(fd)
print(fd2 != fd)   # → True
os.close(fd)
os.close(fd2)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy