Making and breaking file paths in Python#

Pathnames#

A pathname is a string that identifies a particular file or directory on a computer filesystem.

For example, we can ask the pathname of the directory containing this notebook, using the getcwd function from the os module.

import os

os.getcwd()
'/home/runner/work/dipy-textbook/dipy-textbook'

Two ways of manipulating pathnames#

There are two standard ways of manipulating pathnames in Python.

Of the two techniques, the os.path way is rather simpler, but it covers a smaller range of tasks. It can also be more verbose. pathlib does more, and can give you nice-looking, concise code, but it does rely on a particularly Python way of thinking. You will see examples of both in lots of modern code, but we will use pathlib in this textbook, because it will likely be the method you end up using when you are more experienced writing Python code.