Python Glossary Русский

Python 3.4+pathlibfunction

pathlib.Path.cwd()

creation

Return the current working directory as a Path — the pathlib counterpart of `os.getcwd()`, which returns a string.

Syntax

Path.cwd()

The current directory is wherever the program was launched from, not where the .py file lives — run the same script from another folder and every relative path shifts with it. For data sitting next to the script, derive the path from __file__ instead of Path.cwd(). The value is not fixed either: os.chdir changes it process-wide, and relative paths start resolving against the new location.

Examples

from pathlib import Path
Path.cwd() # → PosixPath('/current/dir')
Path.cwd().name # → имя текущего каталога
Path.cwd().is_absolute()  # → True
len(list(Path.cwd().glob('*.py')))  # → число .py-файлов в текущем каталоге
Path('data.txt').absolute() == Path.cwd() / 'data.txt'  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy