Python Glossary Русский

all Python 3osfunction

os.access

os — file system

Check access permissions for a path (R_OK/W_OK/X_OK/F_OK).

Syntax

os.access(path, mode)

It checks permissions for the real, not the effective, user — it was designed for setuid programs. Checking in advance creates a race: the file can be swapped between access and open, and the documentation explicitly advises opening the file and handling the error instead. For root the answer is almost always "allowed", even for write-protected files.

Examples

import os
print(os.access(os.__file__, os.R_OK))   # → True
print(os.access('нет-такого-файла.txt', os.F_OK))   # → False — F_OK это просто проверка существования
print(os.access(os.__file__, os.W_OK))   # → зависит от прав установки: True, если файл стандартной библиотеки доступен на запись
lib_dir = os.path.dirname(os.__file__)
print(os.access(lib_dir, os.R_OK | os.X_OK))   # → True — каталог можно читать и входить в него, режимы объединяются через |
print(os.access('нет-такого-файла.txt', os.R_OK))   # → False — access не бросает исключение, в отличие от os.stat

See also

Open in the full glossarydocs.python.orgReport an inaccuracy