Get and Change the Current Working Directory in Python

Modified: | Tags: Python, File

In Python, you can get and change (set) the current working directory using os.getcwd() and os.chdir().

The os module is part of Python’s standard library, so no additional installation is required. However, you do need to import it.

You can obtain the path of the currently executing script file (.py) using __file__. For more information, refer to the following article.

Get the current working directory with os.getcwd()

os.getcwd() returns the absolute path of the current working directory as a string (str).

getcwd stands for "get current working directory", while the Unix command pwd means "print working directory". You can print the current working directory by passing the result of os.getcwd() to the print() function.

import os

path = os.getcwd()

print(path)
# /Users/mbp/Documents/my-project/python-snippets/notebook

print(type(path))
# <class 'str'>

For information on manipulating path strings, see the following article.

Change the current working directory with os.chdir()

You can change (set) the current working directory with os.chdir().

os.chdir() changes the current directory, similar to the Unix command cd. Both chdir and cd stand for "change directory".

Provide the destination path as an argument. It can be either absolute or relative. Use .. to move up one directory level.

os.chdir('..')

print(os.getcwd())
# /Users/mbp/Documents/my-project/python-snippets

os.chdir('notebook/data')

print(os.getcwd())
# /Users/mbp/Documents/my-project/python-snippets/notebook/data

You can change the current directory to the one containing the running script file (.py) using __file__ and functions from the os.path module.

os.chdir(os.path.dirname(os.path.abspath(__file__)))
source: file_path.py

See the following article for details.

Get and change the current working directory with pathlib

The pathlib module provides object-oriented tools for filesystem path manipulation and is part of the standard library.

The class method Path.cwd() returns a Path object representing the current directory's absolute path.

from pathlib import Path

p = Path.cwd()
print(p)
# /Users/mbp/Documents/my-project/python-snippets/notebook

print(type(p))
# <class 'pathlib.PosixPath'>

Depending on the OS, an instance of PosixPath or WindowsPath is created. For the basics of Path, refer to the following article.

Although not explicitly documented, cwd() can still be called from a Path instance.

print(p.cwd())
# /Users/mbp/Documents/my-project/python-snippets/notebook

Use os.chdir() to change the current directory since pathlib lacks a direct method. A Path object can be directly specified as an argument to os.chdir().

import os

os.chdir(p.parent)

print(Path.cwd())
# /Users/mbp/Documents/my-project/python-snippets

os.chdir(p / 'data')

print(Path.cwd())
# /Users/mbp/Documents/my-project/python-snippets/notebook/data

As shown above, you can use the parent attribute to obtain a Path object for the parent directory, and use the / operator to join paths.

Related Categories

Related Articles