Python: Working with Paths & the Filesystem

Last updated:
Table of Contents

All examples assume Python 3.6+ running on a Unix-based system (unless otherwise stated)

Relative to absolute path (current file location)

This will not warn you if the resolved path is not valid!

To convert a path relative to where the current file is located.

Use os.path.abspath, and prepend __file__+"/" to the relative path:

import os

# path is relative to the directory where THIS file
# is located!
abs_path = os.path.abspath(__file__+"/../../other-dir")

Relative to absolute path (working dir)

This will not warn you if the resolved path is not valid!

See also Relative to absolute path (current file location)

To convert a path relative to the directory you called the script from (in other words, from your current working dir)

import os

# path is relative to the current WORKING directory!
abs_path = os.path.abspath("../../other-dir")

Relative to absolute path in Jupyter

"NameError: name '__file__' is not defined"

In jupyter notebooks, you must use "__file__" (in quotes) instead:

import os

# on a jupyter notebook
abs_path = os.path.abspath("__file__"+"/../../other-dir")

File name from path

Use os.path.basename(<full-path-to-file>)

import os

os.path.basename("/path/to/some/file.txt")
# >>> "file.txt"

Directory path from file name

The trailing slash is not included.

Just use os.path.dirname(<full-path-to-file>):

import os

print(os.path.dirname("/path/to/some/file.txt"))
# >>> "/path/to/some"

Check if path is relative

This does not check if the path exists!

To check if a given string is an absolute path, use os.path.isabs(path_string):

import os

# relative path
os.path.isabs("../../foo/bar")
# >>> False

# relative path
os.path.isabs("foo/bar")
# >>> False

# absolute path
os.path.isabs("/foo/bar")
# >>> True

Current working directory

The current working directory (also called CWD) is the directory the script was called from.

import os

os.getcwd()

Current file directory

Not the current working directory (seen above)

To get the directory where the file is located:

import os

# __file__ points to the where the current file is located
os.path.dirname(os.path.realpath(__file__))

Add relative path to import path

To add a relative path to the import path, use sys.path.insert() and os,path.abspath() as follows:

import os
import sys

sys.path.insert(0, os.path.abspath(__file__+"../relative/path/to/mymodule"))
# you can now import anything from mymodule

Add path to import path

Use sys.path.insert(0,absolute_path_to_directory)

import sys

sys.path.insert(0, "/absolute/path/to/mymodule")
# you can now import stuff from mymodule

Add directory to system PATH

That is, the PATH environment variable, not the python import path!

import os

os.environ["PATH"] += os.pathsep + "/path/to/directory"

"NameError: name '__file__' is not defined"

This usually happens in jupyter notebooks. See Relative to absolute path in Jupyter notebooks

Dialogue & Discussion