Python Glossary Русский

all Python 3Windowsosfunction

os.startfile()

os — running programs

Open a file or folder with its associated application, like double-clicking it in Explorer; does not wait. Windows only.

Syntax

os.startfile(path[, operation][, arguments][, cwd][, show_cmd])

It returns as soon as the program is launched and reports nothing about how it ended — there is no way to wait for it. The operation parameter picks the action: 'open' by default, 'print', 'edit'. macOS and Linux have no counterpart in os: use subprocess.run(['open', path]) and subprocess.run(['xdg-open', path]) respectively.

Examples

import os, sys
print(hasattr(os, 'startfile') == (sys.platform == 'win32'))   # → True
print(not hasattr(os, 'startfile') or callable(os.startfile))   # → True
opener = getattr(os, 'startfile', None)   # None на Linux/macOS
print(opener is None or 'startfile' in repr(opener))   # → True
import os, sys
# os.startfile('report.pdf')   # Windows: открыть файл в ассоциированном приложении

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy