Path.stat
p.stat().st_size is the file size in bytes, st_mtime the modification time as a Unix timestamp. Most fields are machine-specific, so this page has no live demo; the examples use only portable facts.
Common call
p.stat().st_size
Returns
os.stat_result (st_size, st_mtime, st_mode, …)
Replaces
os.stat(p), os.path.getsize(p), os.path.getmtime(p)
Watch out
owner() / group() raise UnsupportedOperation on Windows
Path.stat(*, follow_symlinksfollow_symlinks — stat (3.10+), owner / group (3.13+): with False, describe a symlink itself (stat(follow_symlinks=False) is lstat()).type: bool · default: True=True) / .lstat() / .owner(*, follow_symlinksfollow_symlinks — stat (3.10+), owner / group (3.13+): with False, describe a symlink itself (stat(follow_symlinks=False) is lstat()).type: bool · default: True=True) / .group(*, follow_symlinksfollow_symlinks — stat (3.10+), owner / group (3.13+): with False, describe a symlink itself (stat(follow_symlinks=False) is lstat()).type: bool · default: True=True) / .samefile(other_path)
→ os.stat_result | str | bool
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| follow_symlinks | bool | no (True) | stat (3.10+), owner / group (3.13+): with False, describe a symlink itself (stat(follow_symlinks=False) is lstat()). |
| other_path | str | os.PathLike | yes | samefile only: the path to compare with. |
Return value
os.stat_result | str | bool — stat/lstat: os.stat_result; owner/group: a user or group name; samefile: bool.
Common patterns
Human-readable size
st_size is bytes.
from pathlib import Path size_mb = Path('video.mp4').stat().st_size / 1024 ** 2
Modification time as a datetime
st_mtime is seconds since the epoch.
from datetime import datetime, timezone from pathlib import Path modified = datetime.fromtimestamp(Path('report.pdf').stat().st_mtime, tz=timezone.utc)
Files changed since a timestamp
Compare mtimes while globbing.
from pathlib import Path changed = [p for p in Path('src').rglob('*.py') if p.stat().st_mtime > last_build]
Is it executable? (POSIX)
Test the mode bits with the stat module.
import stat from pathlib import Path is_exec = bool(Path('run.sh').stat().st_mode & stat.S_IXUSR)
Examples
1. Size in bytes
from pathlib import Path
p = Path('data.bin')
p.write_bytes(b'12345')
p.stat().st_size
Returns
52. An empty file
from pathlib import Path
Path('empty').touch()
Path('empty').stat().st_size
Returns
03. Mode bits say file or folder
import stat
from pathlib import Path
Path('d').mkdir()
Path('f').touch()
(stat.S_ISDIR(Path('d').stat().st_mode), stat.S_ISREG(Path('f').stat().st_mode))
Returns
(True, True)4. lstat of a regular file equals stat
from pathlib import Path
p = Path('x')
p.touch()
p.lstat().st_size == p.stat().st_size
Returns
True5. samefile ignores spelling
from pathlib import Path
Path('sub').mkdir()
Path('f.txt').touch()
Path('sub/../f.txt').samefile('f.txt')
Returns
True6. Missing file
from pathlib import Path
try:
Path('ghost').stat()
except OSError as e:
result = type(e).__name__
result
Returns
'FileNotFoundError'Pitfalls
1. Measuring text files written with write_text
write_text translates '\n' to os.linesep, so the byte size of the same text differs between Windows and Linux. Compare characters, or write bytes.
size after write_text
import os from pathlib import Path p = Path('t.txt') p.write_text('a\nb\n', encoding='utf-8') p.stat().st_size == 4 + 2 * (len(os.linesep) - 1)
True
newline=""
from pathlib import Path p = Path('t.txt') p.write_text('a\nb\n', encoding='utf-8', newline='') p.stat().st_size
4
2. Calling owner() on Windows
owner() and group() need the POSIX pwd / grp modules. Guard them in portable code.
unguarded
import os from pathlib import Path, UnsupportedOperation Path('f').touch() try: Path('f').owner() result = 'name' except UnsupportedOperation: result = 'unsupported' result == ('unsupported' if os.name == 'nt' else 'name')
True
guarded
import os from pathlib import Path Path('f').touch() owner = Path('f').owner() if os.name != 'nt' else None owner is None or isinstance(owner, str)
True
When to use
Use it
- File size, modification time, mode bits
- Checking whether two paths are the same file (samefile)
Reach for something else
- Only need "does it exist / is it a file" → exists() / is_file()
- Many files in one folder → os.scandir entries cache stat data
Notes
CPython impl
stat is os.stat(self, follow_symlinks=…) (Lib/pathlib/_local.py); lstat, owner, group and samefile come from PathBase in _abc.py; owner/group are only defined when pwd / grp import
Portable fields
st_size, st_mtime, st_atime, st_mode (file type bits) are meaningful everywhere; st_ino, st_dev, st_uid, st_gid, st_ctime differ by OS
st_ctime
Means different things per OS (metadata change time on POSIX) — do not use it as a portable creation time
samefile
Compares st_ino and st_dev of both paths, so hard links and different spellings of one file are the same
FAQ
Path('file').stat().st_size — the size in bytes. os.path.getsize(p) is the same number.