Library contents

Python Path.stat

Measure how many bytes a saved receipt occupies before attaching it to a message.

A simple example

Path.stat

  1. Start with A regular file containing ASCII AB
  2. Do this Measure its length
  3. Result 2 bytes
Each ASCII character occupies one byte and this file has no trailing newline.

Syntax

Python syntax
path.stat().st_size
Run these examples in Python

Save one example at a time as lesson.py. Use Python 3.9 or later.

python3 lesson.py

Check a receipt attachment size

An office is preparing receipts for transfer and needs to budget storage. Identify which size the program measures, especially when a receipt is also stored inside a compressed archive.

From the programpath.write_text("milk", encoding="utf-8")
Result4
lesson.py
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as folder:
    path = Path(folder) / "today.txt"
    path.write_text("milk", encoding="utf-8")
    print(path.stat().st_size)
Output
4

The receipt contains the four ASCII bytes in milk. No newline is written.

Advertisements

Prepare a gallery label

A curator stores a short UTF-8 caption beside an exhibit. Read the size field on the saved file. How many bytes did these ASCII characters occupy without a trailing newline?

GivenFile contents “clay”
Worked result4

Prepare a studio handover

A stage manager saves a two-word instruction for the next shift. Read the size field on the saved file. How many bytes did these ASCII characters occupy without a trailing newline?

GivenFile contents “ring bell”
Worked result9

What to watch for

File sizes are measured in bytes. Text character counts can differ.

Documentation for Path.stat