On this page
shutil.which
Prev: shutil.unregister_unpack_format | Up: shutil
Rel: shutil.chown, shutil.copy, shutil.copy2, shutil.copyfile
Ref: docs.python.org — shutil.which
Tags: #public #python3 #stdlib #shutil
Import
import shutil
from shutil import which
Signature & Overview
shutil.which(cmd, mode=1, path=None)
Given a command, mode, and a PATH string, return the path which conforms to the given mode on the PATH, or None if there is no such file.
Usage
import shutil
py = shutil.which("python3")
assert py is None or isinstance(py, str)
Gotchas
- Returns
Nonewhen not found: Much cleaner and more portable than shelling out to Unixwhichor Windowswhere. - Checks
PATH(andPATHEXTon Windows): Passpath=explicitly to search a custom directory list instead ofos.environ["PATH"].
REPL Inspect
from shutil import which
help(which)
Linked from
Related notes
- tuts/python3/stdlib_module/shutil/unregister_unpack_format Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shutil/chown Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copy Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copy2 Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copyfile Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copyfileobj shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copymode shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copystat shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copytree shared tags: #shutil, #stdlib