How to Get the Path of Current Working Directory in Python?

Get the path of the current working directory in Python

In this article, we will discuss how to get the path of the current working directory in python. First, let us understand what a directory is.

Directory

The directory or simply say folder is a collection of files and sub-directory. A directory or folder is created to store our files in an organized manner.

Path of Directory

The path of the directory is specified as a unique location of the directory in the system. Unique here identifies that no two files or directories can have the same name in the same path or location. There are 2 types of path one is an absolute path while the other is a relative path.

Absolute path

The path with reference to the root directory is called the absolute path.

Relative Path

The path with reference to the current directory is called the relative path.

os.getcwd() Method

This can be achieved through the os module in python. Before using this module we have to import this module into the program and then we are free to use os module functions.

getcwd() function of os module helps us to find the path current working directory. As this function returns the path of current working directly so we can either store it in a variable then print it or we can directly print it as per our requirement.

syntax: variable_name = os.getcwd()

import os
print(os.getcwd())

Output

C:\Users\HP\Desktop\python

So we can easily get the path of our current working directly using this method.

Now the question that may arise is this path is an absolute path or a relative path. As we get the path with respect to the root directory hence os. getcwd() method gives the absolute path.

Leave a Comment