Python Glossary Русский

all Python 3statfunction

stat.S_IMODE()

mode bits

Extracts the permission bits from st_mode (rwx plus setuid, setgid and sticky), dropping the file type.

Syntax

stat.S_IMODE(mode)

The result is a plain int, and print() shows it in decimal (0o750 prints as 488): wrap it in oct() to read it. Unlike `mode & 0o777`, S_IMODE masks with 0o7777 and keeps setuid/setgid/sticky. On Windows os.chmod() only toggles the read-only flag, so permissions there are always 0o666 or 0o444.

Examples

import os, stat
open('run.sh', 'w').close()
os.chmod('run.sh', 0o750)
mode = os.stat('run.sh').st_mode
print(stat.S_IMODE(mode))  # → 488 — то же число, но десятичным (на Windows 438)
print(oct(stat.S_IMODE(mode)))  # → 0o750 (на Windows chmod меняет только флаг «только чтение»)
import stat
mode = 0o104755  # обычный файл с битом setuid
print(oct(mode & 0o777), oct(stat.S_IMODE(mode)))  # → 0o755 0o4755

See also

Open in the full glossarydocs.python.orgReport an inaccuracy