All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Get Current Working Directory

Last Updated : Mar 11, 2024

Python Get Current Working Directory

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.

  1. from pathlib import Path
  2. current_working_directory = Path.cwd()
  3. print("The location of the current working directory is:")
  4. print(current_working_directory)
  5. 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.
  6. The current working directory's path is then obtained using the cwd() module.
  7. For print that current working directory in which the script is running, utilise the cwd() function of the Path class.
  8. 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())
  1. Lets write import os in the initial line of code to import the os module.
  2. Next, using the getcwd() function, we utilise the print method to print the current working directory.
  3. 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.
  4. The chdir() method is then used to modify the current directory path.
  5. 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)
  1. A first code line contains the statement import os, which imports the os module.
  2. The realpath() function is then used to read the currently open working directory.
  3. It accepts the value of the parameter __file__, which contains the file's pathname where os package is imported.
  4. Next, we output the script name and current working directory using the print function.

Conclusion :-

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.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪