ZetCode

Python get current working directory

last modified February 23, 2021

Python get current working directory tutorial shows ways how to find out the current working directory in Python.

Python tutorial is a comprehensive tutorial on Python language.

Current working directory is a full path wheare a program is executed.

$ pwd
/janbodnar/Documents/prog/python/getcwd

We can find out the current working directory with the pwd command.

There are several ways of finding the current working directory in Python. We can use the following methods:

Get current working directory with os.getcwd

The os.getcwd returns a string representing the current working directory.

os_getcwd.py
#!/usr/bin/python

import os

w_dir = os.getcwd()
print(w_dir)

The program prints the current working directory with os.getcwd.

$ ./os_getcwd.py 
/janbodnar/Documents/prog/python/getcwd

Get current working directory with Path.cwd

The Path.cwd returns a new path object representing the current directory.

path_cwd.py
#!/usr/bin/python

from pathlib import Path

work_dir = Path.cwd()
print(work_dir)

The program prints the current working directory with Path.cwd.

Get current working directory with os.path

The __file__ is a special Python build-in variable which contains the path to the currently running script. Since Python 3.9, the value is an absolute path. In earlier versions, the path could be relative.

When traversing file system hierarchy, we can use the . for current working directory, and the .. for parent directory.

os_path.py
#!/usr/bin/python

import os

print(os.path.dirname(os.path.normpath(__file__)))
print(os.path.abspath('.'))

The os.path.dirname returns the directory name of the given path. The normpath method normalizes a pathname by collapsing redundant separators and up-level references. The path.abspath returns a normalized absolutized version of the given pathname.

In this tutorial, we have shown ways of finding the current working directory in Python.

Read Python tutorial or list all Python tutorials.