Getting and Converting Absolute and Relative Paths in Python: Usage of os.path.abspath and relpath

In programs that perform file operations, we use a “Path” to specify the file location. There are two types of paths: “Absolute Path,” which describes the location from the root directory, and “Relative Path,” which describes the location based on the current position (current directory).

Using Python’s standard library os.path module, it is possible to convert between these formats or calculate the relative position from a specific location. This article explains how to use os.path.abspath() and os.path.relpath().


目次

1. Getting an Absolute Path: os.path.abspath()

An “Absolute Path” is the complete address of a file or directory, starting with a drive letter (Windows) or the root (macOS/Linux). Using the os.path.abspath() function, you can convert a relative path (like just a filename or .) into an absolute path based on the current working directory.

Syntax:

import os

absolute_path = os.path.abspath(path)

Specific Usage Example

Here is an example of getting the full path of a configuration file located in the same hierarchy as the script.

import os

# Relative path ('settings.yaml' in the current directory)
relative_file_path = "settings.yaml"

# Convert to absolute path
full_path = os.path.abspath(relative_file_path)

print(f"Input: {relative_file_path}")
print(f"Absolute Path: {full_path}")

Output Example:

Input: settings.yaml
Absolute Path: /Users/username/projects/app/settings.yaml

(On Windows, it would look like C:\Users\username\projects\app\settings.yaml)

This is very important when you want to uniquely identify and record the location of a file, such as when outputting logs or registering to a database.


2. Getting a Relative Path: os.path.relpath()

A “Relative Path” is the location of a destination viewed from a certain point. Using the os.path.relpath() function, you can calculate and obtain the route to move from a “certain directory (start)” to the “target path (path)”.

Syntax:

relative_path = os.path.relpath(target_path, start_path)

Note: If the second argument (start_path) is omitted, the current directory becomes the start point.

Specific Usage Example

Here is an example of calculating the positional relationship between two different directories. This is useful, for example, when generating a link path from an HTML file to an image file in website construction.

import os

# Target path (Image folder)
target_path = "/var/www/html/assets/images/logo.png"

# Start path (Current page)
start_path = "/var/www/html/pages/about"

# Calculate relative path from start_path to target_path
relative_route = os.path.relpath(target_path, start_path)

print(f"Target: {target_path}")
print(f"Start:  {start_path}")
print(f"Relative Path: {relative_route}")

Output (macOS/Linux):

Target: /var/www/html/assets/images/logo.png
Start:  /var/www/html/pages/about
Relative Path: ../../assets/images/logo.png

You can see that the correct path is generated using .. (parent directory) to go up the hierarchy.


Summary

  • os.path.abspath(path): Converts a relative path to an “Absolute Path.” It is based on the current working directory.
  • os.path.relpath(path, start): Calculates the “Relative Path” from start to path.

These functions are essential in file system operations when you need to unify path formats or dynamically generate links.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次