Python Glossary Русский

all Python 3osfunction

os.chdir

os — directories

Change the process's current working directory.

Syntax

os.chdir(path) -> None

It changes the directory for the whole process — every thread and library included — so avoid it in multithreaded programs and library code. When the change is temporary use contextlib.chdir (Python 3.11+): it restores the previous directory even on an exception. Passing absolute paths often removes the need to change directory at all.

Examples

import os
cwd = os.getcwd()
os.chdir(cwd)
print(os.getcwd() == cwd)   # → True
start = os.getcwd()
os.chdir('/tmp')
print(os.getcwd())  # → /tmp
os.chdir(start)  # вернуться в исходный каталог

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy