On this page
stat
Prev: ssl | Up: stdlib_module | Next: statistics
Rel: pathlib, os, io, time, fileinput
Ref: docs.python.org โ stat
Tags: #public #python3 #stdlib
Import
import stat
Overview
Constants and helper functions for interpreting os.stat() / Path.stat() st_mode bitmasks (S_ISREG, S_ISDIR, S_ISLNK, S_IMODE, filemode, S_IRUSR, S_IWUSR, S_IXUSR). Use stat to inspect POSIX permissions, format ls -l-style mode strings, or set executable/owner-only permissions cleanly without opaque octal bit math.
Quick Start
import os
from pathlib import Path
import stat
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "secret.key"
p.write_text("token", encoding="utf-8")
p.chmod(stat.S_IRUSR | stat.S_IWUSR) # 0o600 (owner read/write only)
mode = p.stat().st_mode
assert stat.S_ISREG(mode) and stat.S_IMODE(mode) == 0o600
assert stat.filemode(mode) == "-rw-------"
Implementation Walkthrough
1. Marking a Generated Script Executable (Preserving Existing Bits)
from pathlib import Path
import stat
def make_executable(path: Path) -> None:
current = path.stat().st_mode
path.chmod(current | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
Gotchas & Boundaries
- Use
Path.lstat()(oros.lstat()) to detect symlinks:stat()follows symlinks, sostat.S_ISLNK(p.stat().st_mode)is alwaysFalse. - On Windows, POSIX permission bits are mostly ignored beyond the read-only flag (
S_IWRITE). - Always mask with
stat.S_IMODE(st_mode)when comparing permission bits (like0o600) so file-type bits don't break equality.
Common Functions
- stat.filemode() โ
filemode(object, /)โ Convert a file's mode to a string of the form '-rwxrwxrwx'
Constants & Attributes
stat.FILE_ATTRIBUTE_ARCHIVE(int) =32stat.FILE_ATTRIBUTE_COMPRESSED(int) =2048stat.FILE_ATTRIBUTE_DEVICE(int) =64stat.FILE_ATTRIBUTE_DIRECTORY(int) =16stat.FILE_ATTRIBUTE_ENCRYPTED(int) =16384stat.FILE_ATTRIBUTE_HIDDEN(int) =2stat.FILE_ATTRIBUTE_INTEGRITY_STREAM(int) =32768stat.FILE_ATTRIBUTE_NORMAL(int) =128stat.FILE_ATTRIBUTE_NOT_CONTENT_INDEXED(int) =8192stat.FILE_ATTRIBUTE_NO_SCRUB_DATA(int) =131072stat.FILE_ATTRIBUTE_OFFLINE(int) =4096stat.FILE_ATTRIBUTE_READONLY(int) =1stat.FILE_ATTRIBUTE_REPARSE_POINT(int) =1024stat.FILE_ATTRIBUTE_SPARSE_FILE(int) =512stat.FILE_ATTRIBUTE_SYSTEM(int) =4
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/fileinput
- tuts/python3/stdlib_module/io
- tuts/python3/stdlib_module/os
- tuts/python3/stdlib_module/pathlib
- tuts/python3/stdlib_module/pwd
- tuts/python3/stdlib_module/ssl
- tuts/python3/stdlib_module/stat/filemode
- tuts/python3/stdlib_module/statistics
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/time
Related notes
- tuts/python3/stdlib_module/fileinput Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/io Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/os Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pathlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ssl Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stat/filemode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/statistics Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/time Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib