Size of a file

EasyFile Handling Programs · 21 of 38

Write a Python program to get the size of a file in bytes.

Advertisement
Output
1500 bytes

Approach

  1. os.path.getsize().

Solution

Python
import os with open("data.bin", "wb") as f:    f.write(b"x" * 1500) print(os.path.getsize("data.bin"), "bytes")
Output
1500 bytes

Line by line

Line 3

Writing in binary mode makes the size exact on every operating system.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs