Python Glossary Русский

all Python 3osfunction

os.path.getsize

os.path — metadata (stat)

Return the size of a file in bytes.

Syntax

os.path.getsize(path)

For a directory it returns the size of the directory entry itself (usually 4096 on Linux), not the total of its contents — that needs a walk. A missing path raises FileNotFoundError rather than returning 0. A sparse file's size can exceed the space it actually occupies on disk.

Examples

import os
print(os.path.getsize(os.__file__) > 0)   # → True
print(os.path.getsize(os.__file__) == os.stat(os.__file__).st_size)   # → True — getsize это os.stat(path).st_size
tmp = 'grader_demo.txt'   # файл в текущем каталоге
with open(tmp, 'w', encoding='utf-8') as f: f.write('hello')
print(os.path.getsize(tmp))   # → 5
print(round(os.path.getsize(os.__file__) / 1024, 1))   # → ? размер в килобайтах, например 42.3
print(os.path.getsize(os.path.dirname(os.__file__)))   # → ? служебный размер записи каталога (на Windows обычно 0), а НЕ сумма файлов внутри

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy