Dr. Kickass Dr. Kickass - 3 years ago 103
Python Question

Why no len(file) in Python?

I'm not exactly new to Python, but I do still have trouble understanding what makes something "Pythonic" (and the converse).

So forgive me if this is a stupid question, but why can't I get the size of a file by doing a len(file)?


is not even implemented, so it's not like it's needed for something else? Would it be confusing/inconsistent for some reason if it was implemented to return the file size?

Answer Source

file is an iterator. To find the number of lines you need to read the entire file

sum(1 for line in file)

if you want the number of bytes in a file, use os.stat


import os
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download