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:

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- Use your iPhone microphone as a mic on macOS Ventura - MacOS
- Calculate Volume of Cone - C-Program
- The default interactive shell is now zsh. [macOS] - MacOS
- Call a Stored Procedure using Java JDBC CallableStatement Example - Java
- How to word wrap in HTML - Html
- Step-by-Step Guide: How to Fix - Error Establishing a Database Connection in WordPress - PHP
- Change Line Endings (Encoding Windows/Mac/Unix CR/LF/CRLF) Sublime Text - Sublime-Text
- Not receiving email notification alert in SharePoint Online workflow - Power Automate, FLOW - SharePoint