Python: Working with Paths & the Filesystem
Last updated:- Absolute path of current file
- Relative to absolute path (file location)
- Relative to absolute path (working dir)
- Relative to absolute path in Jupyter
- File name from path
- Directory path from file name
- Check if path is relative
- Current working directory
- Current file directory
- Add relative path to import path
- Add path to import path
- Add directory to system PATH
- NameError: name '__file__' is not defined
All examples assume Python 3.6+ running on a Unix-based system (unless otherwise stated)
Absolute path of current file
import os
os.path.abspath(__file__)
Relative to absolute path (file location)
This will not warn you if the resolved path is not valid!
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!
Relative to the directory you called the script from.
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
Trailing slash is not included.
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
Trailing slash is not included.
The directory where the file is located:
import os
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