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
- To get the full path of the given file we have used pathlib.Path(“my_file.txt”).parent.absolute().
- And to get the full path of the current working directory we have used pathlib.Path().absolute().
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
- In this example we have used os.path.dirname(‘filename’) to get the path of the filename.
- In this example we have used os.path.abspath(os.getcwd()) to get the path of the current working directory.
Check out our other python programming examples…