Python Glossary Русский

all Python 3Linux · macOSosfunction

os.fchdir

os — directories

Change the working directory to the one given by an open fd. Availability: Unix.

Syntax

os.fchdir(...)

The descriptor must refer to an open directory, not a file. It helps to return to a directory even if it was renamed or its path became unreachable: the descriptor holds the directory itself, not a path. Since Python 3.3 os.chdir(fd) does the same.

Examples

import os
back = os.open('.', os.O_RDONLY)   # → ? только Unix: дескриптор текущего каталога — «закладка», чтобы вернуться
os.chdir('/tmp')   # → ? текущий каталог сменён на /tmp (обычный переход по пути)
os.fchdir(back)   # → ? только Unix: возврат в исходный каталог по дескриптору, а не по строке пути
os.close(back)   # → ? дескриптор освобождён; следующий os.fchdir(back) даст OSError: Bad file descriptor
os.fchdir(os.open('/etc/hosts', os.O_RDONLY))   # → NotADirectoryError: нужен дескриптор каталога, а не файла

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy