Using the os.path module#

The os.path module is one of two ways of manipulating and using file paths in Python. See the path manipulation page for more discussion, and pathlib page for a tutorial on the alternative pathlib approach.

The primary documentation for os.path is https://docs.python.org/3/library/os.path.html

This page covers:

  • os.path

  • dirname, basename, join, splitext, abspath

import os.path

In IPython, you can tab complete on os.path to list the functions and attributes there.

The first function we will use from os.path is dirname. To avoid typing os.path all the time, we import os.path with the shortened name op:

import os.path as op
op.dirname
<function posixpath.dirname(p)>

The dirname function gives the directory name from a full file path. It works correctly for Unix paths on Unix machines, and Windows paths on Windows machines:

# On Unix
op.dirname('/a/full/path/then_filename.txt')
'/a/full/path'

You’ll see something like this as output if you run something similar on Windows. So op.dirname('c:\\a\\full\\path\\then_filename.txt') will give:

'c:\\a\\full\\path'

Notice that, on Windows, you need to use double backslashes, because the backslash in a Python string is a way of escaping a character — meaning, to specify you mean exactly that character. Double backslash has the meaning “Yes, I do mean exactly backslash for the next character”.

dirname also works for relative paths, A relative path where the starting directory is relative to the current directory, rather than absolute, in terms of the root of the file system:

# On Unix
op.dirname('relative/path/then_filename.txt')
'relative/path'

Use basename to get the filename rather than the directory name:

# On Unix
op.basename('/a/full/path/then_filename.txt')
'then_filename.txt'

Sometimes you want to join one or more directory names with a filename to get a path. Windows and Unix have different characters to separate directories in a path. Windows uses the backslash: \, Unix uses a forward slash: /. If your code will run on Windows and Unix, you need to take care that you get the right character joining your paths. This is what os.path.join does:

# On Unix
op.join('relative', 'path', 'then_filename.txt')
'relative/path/then_filename.txt'

This also works on Windows. op.join('relative', 'path', 'then_filename.txt') gives output 'relative\\path\\then_filename.txt'.

To convert a relative to an absolute path, use abspath:

# Show the current working directory
os.getcwd()
op.abspath('relative/path/then_filename.txt')
'/home/runner/work/dipy-textbook/dipy-textbook/relative/path/then_filename.txt'

Use splitext to split a path into: the path + filename; and the file extension:

op.splitext('relative/path/then_filename.txt')
('relative/path/then_filename', '.txt')