Python Get Current Working Directory
Last Updated : Apr 9, 2023
IN - Python
In this article we will show you the solution of python get current working directory, when using a file/folder name in a script, it can either utilize the complete path or only the file/folder name.
The full path of a file or folder, starting with the root directory, is referred to as an absolute path.
When a file name appears in a script without its associated pathname, the current working directory also known as the relative path is taken into account as the file’s pathname.
A directory location from which the Python script executes is designated as Current Working Directory in Python.
To obtain the Current Working Directory, there are numerous Python modules available.
Now move to the concept of python get current working directory.
Step By Step Guide On Python Get Current Working Directory :-
Method - To obtain the Current Working Directory, use the pathlib module.
- from pathlib import Path
- current_working_directory = Path.cwd()
- print("The location of the current working directory is:")
- Importing the Path module from the pathlib module marks the beginning of our programming. For reading the script's active working directory, a path class of a pathlib module is used.
- The current working directory's path is then obtained using the cwd() module.
- For print that current working directory in which the script is running, utilise the cwd() function of the Path class.
- then, utilising the print method, Print the working directory's location.
Method - To obtain the Current Working Directory, use the getcwd() function.
import os print("The current working directory is:\n", os.getcwd()) os.chdir('/etc/mail') print("\nThe current working directory after change is:\n", os.getcwd())
- Lets write import os in the initial line of code to import the os module.
- Next, using the getcwd() function, we utilise the print method to print the current working directory.
- The easiest way to determine that current working directory of a script that is now running is to use the getcwd() method of the OS moduleIt doesn't take any parameters and just return a CWD as a string.
- The chdir() method is then used to modify the current directory path.
- The print method changes the directory before calling the getcwd() command once more.
Method - To get Current Working Directory with realpath()
import os real_path = os.path.realpath(__file__) print("\nThe current working directory with the script name is:") print(real_path)
- A first code line contains the statement import os, which imports the os module.
- The realpath() function is then used to read the currently open working directory.
- It accepts the value of the parameter __file__, which contains the file's pathname where os package is imported.
- Next, we output the script name and current working directory using the print function.
Thus, we were able to comprehend the idea behind Python's get Current Working Directory.
We also discovered that there are many Python programmes available to get the Current Working Directory.
Those are Get the Current Working Directory using the pathlib module, Get the Current Working Directory using getcwd(), and Get the Current Working Directory using realpath().
I hope this article on python get current working directory helps you and the steps and method mentioned above are easy to follow and implement.