W3docs

How to get an absolute file path in Python

You can use the os.path.abspath() function to get the absolute file path of a file in Python.

You can use the os.path.abspath() function to get the absolute file path of a file in Python. Here's an example of how you can use it:

Get an absolute file path in Python

import os

file_path = "example.txt"
absolute_path = os.path.abspath(file_path)
print(absolute_path)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

This will give you the absolute path of the file "example.txt" based on the current working directory.