Algorithm
-
Import the
os
module:- Begin by importing the
os
module to access operating system functionalities.
python - Begin by importing the
-
import os
-
Get the current working directory:
- Use the
os.getcwd()
method to obtain the current working directory.
python - Use the
-
current_directory = os.getcwd()
-
Print the full path:
- Display the full path of the current working directory using the
print
function.
python - Display the full path of the current working directory using the
-
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
/Users/username
/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
/Users/username
/Users/username
/Users/username
Demonstration
Python Programing Example to Get the Full Path of the Current Working Directory-DevsEnv