Python get current working directory
last modified January 29, 2024
Python get current working directory tutorial shows ways how to find out the current working directory in Python.
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:
- os.getcwd
- pathlib.Path.cwd
- os.path
Get current working directory with os.getcwd
The os.getcwd
returns a string representing the current working
directory.
#!/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.
#!/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.
#!/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.
Source
Python os module - language reference
In this article we have shown ways of finding the current working directory in Python.
Author
List all Python tutorials.