ZetCode

Python get current working directory

last modified October 30, 2026 Python 3.15

In this article we show how to find out and change the current working directory in Python.

Current working directory

The current working directory is the directory in which a process runs. Every process has one; it is inherited from the parent process, which is usually the shell from which we launch the program. Relative paths, such as data.txt or ../logs, are resolved against the current working directory.

The current working directory is not necessarily the directory where the Python script is located. The two are the same only when we run the script from its own directory.

Python has several ways of working with the current working directory:

Get current working directory with os.getcwd

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

os_getcwd.py
import os

work_dir = os.getcwd()
print(work_dir)
print(type(work_dir))

The program prints the current working directory and the type of the returned value. In the shell, the current working directory is printed with the pwd command. Both show the same directory, because the Python process inherits it from the shell.

$ pwd
/home/user/python
$ python os_getcwd.py
/home/user/python
<class 'str'>

Get current working directory with Path.cwd

The Path.cwd class method returns a new path object representing the current working directory. This is the preferred way in modern Python code, because path objects provide many useful attributes and operators.

path_cwd.py
from pathlib import Path

work_dir = Path.cwd()

print(work_dir)
print(repr(work_dir))
print(work_dir.name)
print(work_dir.parent)
print(work_dir / 'data.txt')

The program prints the current working directory, its representation, its last component, its parent directory, and a path to a file inside it. The / operator joins path components.

$ python path_cwd.py
/home/user/python
PosixPath('/home/user/python')
python
/home/user
/home/user/python/data.txt

Current working directory vs script directory

The __file__ variable contains the path of the currently running script. Since Python 3.9, it is an absolute path for the main script. The directory of the script is not the same thing as the current working directory.

scripts/where.py
import os
from pathlib import Path

print('cwd:       ', Path.cwd())
print('script dir:', Path(__file__).parent)
print('os.path:   ', os.path.dirname(os.path.abspath(__file__)))

The script prints the current working directory and the directory in which the script is located. The Path(__file__).parent and os.path.dirname expressions give the same result. We run the script from three different directories.

$ python scripts/where.py
cwd:        /home/user/python
script dir: /home/user/python/scripts
os.path:    /home/user/python/scripts
$ cd scripts && python where.py
cwd:        /home/user/python/scripts
script dir: /home/user/python/scripts
os.path:    /home/user/python/scripts
$ cd /tmp && python /home/user/python/scripts/where.py
cwd:        /tmp
script dir: /home/user/python/scripts
os.path:    /home/user/python/scripts

Resolving relative paths

Relative paths are interpreted relative to the current working directory. The os.path.abspath function and the Path.absolute and Path.resolve methods turn them into absolute paths.

relative_paths.py
import os
from pathlib import Path

print(os.path.abspath('.'))
print(os.path.abspath('..'))
print(os.path.abspath('data/words.txt'))

print(Path('data/words.txt').absolute())
print(Path('..').resolve())

The . path stands for the current working directory and the .. path for its parent. The abspath function also normalizes the path, so .. is collapsed. The absolute method only prepends the current working directory; the resolve method also eliminates .. components and follows symbolic links. None of them checks whether the file exists.

$ python relative_paths.py
/home/user/python
/home/user
/home/user/python/data/words.txt
/home/user/python/data/words.txt
/home/user

Changing the current working directory

The os.chdir function changes the current working directory of the running process.

change_dir.py
import os

print(os.getcwd())

os.chdir('/tmp')
print(os.getcwd())

os.chdir('..')
print(os.getcwd())

We change the working directory to /tmp and then to its parent, the root directory. The change affects only the Python process and its future child processes; the shell from which we launched the program stays in its original directory.

$ python change_dir.py
/home/user/python
/tmp
/

Temporary change with contextlib.chdir

Since Python 3.11, the contextlib.chdir context manager changes the current working directory temporarily. On exit from the with block, the original directory is restored, even if an exception was raised.

temp_chdir.py
import contextlib
import os

os.makedirs('build', exist_ok=True)

print('before:', os.getcwd())

with contextlib.chdir('build'):
    print('inside:', os.getcwd())

    with open('log.txt', 'w') as f:
        f.write('build started\n')

print('after: ', os.getcwd())
print(os.path.exists('build/log.txt'))

Inside the with block, the build directory is the current working directory, so the log.txt file is created there. After the block, we are back in the original directory. Since the current working directory is shared by all threads of a process, contextlib.chdir is not suitable for multithreaded code.

$ python temp_chdir.py
before: /home/user/python
inside: /home/user/python/build
after:  /home/user/python
True

os.getcwd and the PWD variable

The shell keeps the logical path of the current directory in the PWD environment variable. When the path goes through a symbolic link, it differs from the physical path returned by os.getcwd.

pwd_env.py
import os

print('os.getcwd():', os.getcwd())
print('$PWD:       ', os.environ.get('PWD'))

We create a data symbolic link to the data-2026 directory and run the program from the link. The os.getcwd function asks the operating system, which reports the physical directory. The PWD variable keeps the path through the link, the same one that pwd prints. Note that PWD is maintained by the shell and is not updated by os.chdir.

$ mkdir data-2026
$ ln -s data-2026 data
$ cd data && pwd && python ../pwd_env.py
/home/user/python/data
os.getcwd(): /home/user/python/data-2026
$PWD:        /home/user/python/data

Working directory of child processes

A child process inherits the current working directory of its parent. The cwd parameter of the subprocess functions runs a command in a different directory without changing the directory of the Python program.

subprocess_cwd.py
import subprocess

subprocess.run(['pwd'])
subprocess.run(['pwd'], cwd='/usr/share')
subprocess.run(['ls', '-C'], cwd='scripts')

The first pwd command runs in the inherited directory. The other two commands run in /usr/share and in the scripts subdirectory; a relative cwd value is resolved against the current working directory.

$ python subprocess_cwd.py
/home/user/python
/usr/share
where.py

Source

os.getcwd - Python documentation

In this article we have shown how to find out and change the current working directory in Python.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Python tutorials.