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

Machine Learning Projects

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

Leave a Comment

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