Python Get Current Directory

When working with python files in directories, it is recommended to use absolute file paths. However, if you are dealing with the relative paths then, you must have a better understanding of the current working directory and how to get the current directory in python. The absolute and relative paths are different from each other. 

The relative path always begins from the current directory, while the absolute path represents a file directory location that starts from the root directory.

To get a current working directory, the os python module is used that helps to interact with the operating system. This module is a part of the standard python library and includes all the methods required to find the current working directory.

Sometimes, Python users might want to know the following:

  • How to create a new directory in Python?
  • How to get a list of directories in Python?
  • How to compare two directories in Python?
  • How to check if directory exists in Python?
  • How to create a parent directory in Python?

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related python queries.

In this context, we shall look into how to get the current python directory.

How to Get current directory in Python using os.getcwd() method ?

In order to get the python current directory via the terminal, Open the terminal using the 'Ctrl+Alt+t' keys and make sure that python should be installed on your system. 

Now, import the os python module by using the following command:

>>> import os

Use the getcwd() method that returns a string that prints the absolute path of the current directory. The returned string will not show the slash characters.

>>> os.getcwd()


Let's explain the above method with the help of an example which is mentioned below:

Create a python script file. Open an empty text file and paste the following source code in this file:

# Import os module
import os
# Get current python directory
cwd = os.getcwd()
# display current python directory
print("Current working directory: {0}".format(cwd))
# Display the returned object type
print("os.getcwd() returns an object of type: {0}".format(type(cwd)))

Now, save the above file with the '.py' extension with some suitable name.

In the above source code, the os module is imported at the top of the file, and then using this module gets the current directory path. Execute this python file through the terminal by running the following command:

$ python file-name.py

Here, the file name is 'getdirectory.py'. So, change the above command into the following form:

$ python getdirectory.py

In the output, you will see python current directory on the terminal.

[Need assistance in fixing Linux System errors? We can help you. ]


This article covers how to use the 'os. getcwd()' method to easily get the python current working directory. When you run a Python script, the current working directory is set to the directory from which the script is executed.

The os python module provides a portable way to interact with the operating system. The module is part of the standard Python library and includes methods for finding and changing the current working directory.

Basically, In Python, you can get and change (set) the current working directory with os.getcwd() and os.chdir().

os module is included in the standard library, so no additional installation is required.

To Get the current working directory: os.getcwd()

To Change the current working directory: os.chdir()

How to Get the current working directory: os.getcwd() ?

1. os.getcwd() returns the absolute path of the working directory where Python is currently running as a string str.

2. getcwd stands for "get current working directory", and the Unix command pwd stands for "print working directory".

More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com