HOWTO · Python

Relative Path in Python

Learn how Python absolute and relative paths work, how their base directory affects resolution, and how to construct paths with the standard library.

On this page

A relative path is interpreted from a base directory, usually the process’s current working directory. It does not automatically mean a path relative to the Python script. Use an absolute path when the location should be independent of that base, or build a relative path from a base you choose.

File Path in Python

A file path is made from directory names and a file name. For example, C:\\PythonProjects\\Tutorials\\Paths\\paths.py names a script beneath the C: drive. Windows paths commonly display backslashes (\\), while macOS and Linux paths commonly display forward slashes (/). These are filesystem separators; in Python string literals, backslashes may also introduce escapes. Use raw strings or forward slashes for Windows literals, or build paths with os.path.join() so Python selects the platform separator.

The examples use this layout: Tutorials contains sibling directories named Paths and Strings; paths.py is in Paths, and string.py is in Strings.

Difference Between Windows, Mac, and Linux

A displayed Windows path can look like C:\\PythonProjects\\Tutorials\\Paths\\paths.py; a POSIX path can look like /home/ava/Tutorials/Paths/paths.py. The separators differ, but Python’s path utilities handle the platform-specific form. A path written as a Python string must also follow Python’s string-literal rules, so prefer r"C:\\PythonProjects\\Tutorials", "C:/PythonProjects/Tutorials", or components passed to os.path.join().

Current Working Directory

The current working directory (cwd) belongs to the running process, not to a particular file. os.getcwd() returns that directory. A relative path such as Strings/string.py is resolved from the cwd; changing the cwd changes how that same relative path is interpreted. os.chdir() changes the process-wide cwd, which can affect other code in the process. For scripts and applications, an explicit base is often easier to reason about than changing the cwd.

Absolute Path

An absolute path identifies a location from a filesystem root (or drive on Windows). For a running script, os.path.abspath(__file__) produces an absolute form of the script path when __file__ is available, as it normally is for a file executed as a script. This is different from resolving an arbitrary relative path: that path is normally interpreted from the process’s cwd.

For example, if the cwd is C:\\PythonProjects\\Tutorials, the file Strings\\string.py can be named with the absolute path C:\\PythonProjects\\Tutorials\\Strings\\string.py. Absolute paths are unambiguous on that machine, but often need adjustment when a project moves to another location.

Construct an Absolute Path to string.py in Python

To construct an absolute path, start with the filesystem root and join the directory and file names. os.path.abspath() makes the result absolute on the current machine. Building a path does not create the file; the example only checks that the constructed path is absolute.

import os
root = os.path.abspath(os.sep)
absolute_path = os.path.abspath(
    os.path.join(root, "Tutorials", "Strings", "string.py")
)
print(os.path.isabs(absolute_path))
True

Relative Path

A relative path names a location from a specified base. In everyday Python programs, the base is often the current working directory, but it can also be a directory selected by the program. The base must be clear: Strings/string.py means one thing when the cwd is Tutorials, and another when it is Tutorials/Paths.

If the cwd is C:\\PythonProjects\\Tutorials, the path to the sibling file is Strings\\string.py. If the cwd is C:\\PythonProjects\\Tutorials\\Paths, the same file is reached as ..\\Strings\\string.py, where .. means the parent directory. os.path.join() can combine these components without hard-coding a separator. To resolve a relative path against the script’s directory instead, derive that directory from __file__ and use it as the explicit base; this is useful when a program should locate resources alongside its code regardless of the launch directory.

For example, start with the script’s directory, move to its parent (Tutorials), then append Strings and string.py. That constructs a path based on the script location rather than the process cwd. os.path.abspath() can convert the result to an absolute form. The example hierarchy assumes Paths and Strings are siblings; adjust the components when your project layout differs.

Construct a Relative Path from Paths to Strings in Python

To describe the path from Tutorials/Paths to Tutorials/Strings/string.py, pass both locations to os.path.relpath(). The start argument makes Paths the explicit base, so the resulting relative path is ../Strings/string.py on this layout. This computes a path string from the supplied names; it does not change the process cwd, check whether the destination exists, or access the file. If you resolve the result against a different base, it identifies a different location.

The example uses relative names for the shared directory layout. os.path.relpath() compares them lexically, so the directories and file do not need to exist for this calculation.

import os

paths = os.path.join("Tutorials", "Paths")
string_file = os.path.join("Tutorials", "Strings", "string.py")
relative_path = os.path.relpath(string_file, start=paths)
print(relative_path.replace(os.sep, "/"))

To change the process cwd, consult os.chdir(). To inspect the process cwd, see os.getcwd().