If you have a relative path, i.e. the path relative to the current working directory for a Python Program or a Script and you want to reach the absolute path of a file(s), then you can get to it via the below steps,
- You should already be aware of the relative path of your file(s)
- Get the Path for the current working directory using os.getcwd()
- Join the current working directory path to your relative file path os.path.join() to get to the file(s)
Example:
import os
relative_data_file_path = "data_2023.csv"
relative_metadata_file_path = "meta/metadata_2023.csv"
working_dir_path = os.getcwd()
absolute_data_file_path = os.path.join(working_dir_path,relative_data_file_path)
absolute_metadata_file_path = os.path.join(working_dir_path,relative_metadata_file_path)
print(absolute_data_file_path)
print(absolute_metadata_file_path)
Output:

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!