Python Glossary Русский

all Python 3Linux · macOSosfunction

os.ctermid

os — pseudo-terminals (pty)

Return the path of the controlling terminal. Availability: Unix.

Syntax

os.ctermid(...)

It usually returns just '/dev/tty' — the generic name of the controlling terminal, not a specific device path like /dev/pts/3. os.ttyname on a terminal descriptor gives the specific name. '/dev/tty' can be opened even when the standard streams are redirected — that is how programs ask for passwords.

Examples

import os
print(os.ctermid())   # → ? только Unix: /dev/tty — путь к управляющему терминалу
path = os.ctermid()   # только Unix
print(os.path.exists(path))   # → True, если у процесса есть управляющий терминал
with open(path, 'w') as tty: tty.write('сообщение мимо перенаправленного stdout\n')   # → ? только Unix: строка появится в терминале, даже если stdout перенаправлен в файл
fd = os.open(path, os.O_RDONLY)   # → ? только Unix: OSError (ENXIO), если процесс запущен без терминала — демон, cron, systemd
print(os.isatty(fd))   # → True — дескриптор действительно указывает на терминал

See also

Open in the full glossarydocs.python.orgReport an inaccuracy