Python program to get the current working directory

Python program to get the current working directory:

In this post, I will show you how to get the current working directory in Python. Python os module provides different methods for different operating system related functions. Current working directory is the directory where the program is currently working on.

Python os.getcwd():

getcwd() method is used to get the current working directory. This method is defined in the os module. It is defined as below:

os.getcwd()

It returnes the current working directory as a string.

To use this method, we need to import os module.

Example program:

import os

print('Current working directory: {}'.format(os.getcwd()))

python get current working directory example

If you run this program, it will print the current working directory. If you are running this program in a python file, it will print the directory where the file exists.

You might also like: