Python Glossary Русский

all Python 3Linux · macOSosfunction

os.getuid

os — user/group IDs

Return the process's real user id (UID). Availability: Unix.

Syntax

os.getuid(...)

The real UID is the user who started the process; permissions, however, are checked against the effective UID, which differs for setuid programs. Zero means root. pwd.getpwuid(os.getuid()).pw_name gives the user name — more reliably than getlogin.

Examples

import os
print(os.getuid())  # → ? только Unix: реальный UID текущего процесса, например 1000
print(os.getuid() == 0)  # → ? True, если скрипт запущен от root (например, через sudo), иначе False
print(os.getuid() == os.geteuid())  # → ? True у обычной программы; у setuid-программы реальный и эффективный UID различаются
home = os.path.expanduser('~')
print(os.stat(home).st_uid == os.getuid())  # → ? True: домашний каталог принадлежит текущему пользователю

See also

Open in the full glossarydocs.python.orgReport an inaccuracy