Python Glossary Русский

all Python 3osfunction

os.closerange

os — file descriptors

Close all file descriptors in the range [fd_low, fd_high), ignoring errors.

Syntax

os.closerange(fd_low, fd_high)

Errors are silently ignored, which is why it is used in a child after fork to shed inherited descriptors. The upper bound is excluded. Ordinary code rarely needs it: subprocess closes extra descriptors by itself by default (close_fds=True).

Examples

import os
r, w = os.pipe()   # → два дескриптора одного канала, например 3 и 4
os.closerange(r, w + 1)   # → закрывает и r, и w: верхняя граница не входит в диапазон; тем же приёмом закрывают унаследованные fd перед exec
os.close(r)   # → OSError: [Errno 9] Bad file descriptor — дескриптор уже закрыт
os.closerange(r, w + 1)   # → повторный вызов не падает: ошибки закрытия молча игнорируются
os.closerange(w, w)   # → пустой диапазон [w, w): не закрывает ничего

See also

Open in the full glossarydocs.python.orgReport an inaccuracy