Python Program to Get the Full Path of the Current Working Directory – 2024

Hey guys, in this blog we will see a Python Program to Get the Full Path of the Current Working Directory.

Example 1: Using pathlib module

import pathlib

# path of the given file
print(pathlib.Path("my_file.txt").parent.absolute())

# current working directory
print(pathlib.Path().absolute())

Output

C:\Users\ashar\OneDrive\Desktop\Python Tutorials
C:\Users\ashar\OneDrive\Desktop\Python Tutorials

Example 2: Using the os module

import os

# path of the given file
print(os.path.dirname(os.path.abspath("my_file.txt")))

# current working directory
print(os.path.abspath(os.getcwd()))

Output

C:\Users\ashar\OneDrive\Desktop\Python Tutorials
C:\Users\ashar\OneDrive\Desktop\Python Tutorials

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 514

Leave a Reply

Your email address will not be published. Required fields are marked *