Table of Contents
In this example, you will learn to get the full path of the current working directory.
To understand this example, you should have the knowledge of the following Python programming topics:
1. 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
/Users/username /Users/username
Using the pathlib
module, you can get the current working directory.
- Pass the file’s name in
Path()
method. parent
gives the logical parent of the path andabsolute()
gives the absolute path of the file.pathlib.Path().absolute()
gives the current working directory.
2. Example 2: Using 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
/Users/username /Users/username
You can do the same thing with the os
module.
- Use
abspath()
method to get an absolute path. getcwd()
gives the current working directory.
Related posts:
Python float()
Python Package
Python List copy()
Python Program to Split a List Into Evenly Sized Chunks
Python Program to Measure the Elapsed Time in Python
Python dict()
Python zip()
Python Set isdisjoint()
Python Program to Convert Two Lists Into a Dictionary
Python ascii()
Python Program to Display Powers of 2 Using Anonymous Function
Python reversed()
Python String capitalize()
Python Program to Remove Punctuations From a String
Python String rsplit()
Python sum()
Python eval()
Python Program to Create a Long Multiline String
Python del Statement
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Find ASCII Value of Character
Python Namespace and Scope
Python Program to Illustrate Different Set Operations
Python len()
Python Statement, Indentation and Comments
Python set()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python staticmethod()
Python Program to Check the File Size
Python Program to Find LCM
Python Set difference_update()
Python Tuple count()