Algorithm


  1. Import the os module:

    • Begin by importing the os module to access operating system functionalities.
    python
  2. import os
  3. Get the current working directory:

    • Use the os.getcwd() method to obtain the current working directory.
    python
  4. current_directory = os.getcwd()
  5. Print the full path:

    • Display the full path of the current working directory using the print function.
    python
  6. print("Current Working Directory:", current_directory)

 

Code Examples

#1 Code Example- Python Programing Using pathlib module

Code - Python Programming

import pathlib

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

# current working directory
print(pathlib.Path().absolute())
Copy The Code & Try With Live Editor

Output

x
+
cmd
/Users/username
/Users/username

#2 Code Example- Python Programing Using os module

Code - Python Programming

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()))
Copy The Code & Try With Live Editor

Output

x
+
cmd
/Users/username
/Users/username
Advertisements

Demonstration


Python Programing Example to Get the Full Path of the Current Working Directory-DevsEnv