Python Glossary Русский

Python 3.3+Linux · macOSosfunction

os.getgrouplist

os — user/group IDs

Return the group ids a user belongs to (by name and base GID). Availability: Unix.

Syntax

os.getgrouplist(...)

Unlike getgroups it answers about a user from the group database, not about the current process: getgrouplist('alice', gid). So it also sees recently assigned groups. The second argument is the user's primary group, which is included in the result.

Examples

import os
print(os.getgrouplist('root', 0))  # → ? только Unix: список GID групп пользователя root, например [0]
print(27 in os.getgrouplist('root', 0))  # → True, если root состоит в группе с GID 27 (sudo в Ubuntu)
print(len(os.getgrouplist('root', 0)) >= 1)  # → True — в отличие от os.getgroups(), спросить можно про любого пользователя, не только текущего
print(os.getgrouplist('нет-такого-пользователя', 100))  # → ? только Unix: обычно [100] — неизвестное имя не ошибка, вернётся только базовый GID

See also

Open in the full glossarydocs.python.orgReport an inaccuracy