visibility 632 comment 0 access_time 6 months ago language English

codePython Convert Relative to Absolute File Path

This code snippet shows you how to convert a relative path to absolute path using os module in Python and vice versa. 

We utilize os.path.abspath and os.path.relpath functions. For the later, it accepts the second parameter as the base path which will be used to calculate the relative path.


*The current directory when running this code is C:\Users\userid.


Code snippet

import os
relative_path = "../test.csv"
absolute_path = os.path.abspath(relative_path)
print(os.path.relpath(absolute_path, "C:\\Users\\myfolder"))
fork_right Fork
copyright This page is subject to Site terms.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

comment Comments
No comments yet.