What is the intended way to change directory using the Python pathlib
(Documentation) functionality?
Lets assume I create a Path
object as foll
In the Python 3.6 or above, os.chdir()
can deal with Path
object directly. In fact, the Path
object can replace most str
paths in standard libraries.
os.chdir(path) Change the current working directory to path.
This function can support specifying a file descriptor. The descriptor must refer to an opened directory, not an open file.
New in version 3.3: Added support for specifying path as a file descriptor on some platforms.
Changed in version 3.6: Accepts a path-like object.
import os
from pathlib import Path
path = Path('/etc')
os.chdir(path)
This may help in the future projects which do not have to be compatible with 3.5 or below.