There are actually three ways I have in mind to determine a files size:
So what is the actual right way to determine the file size? What is the worst way to do it? Or doesn't it even matter, because at the end it is all the same?
(I can imagine the first method having a problem with really large files, while the two others have not.)
The first method would be a waste if you don't need the contents of the file anyway. Either of your other two options are fine. os.path.getsize()
uses os.stat()
From genericpath.py
def getsize(filename):
"""Return the size of a file, reported by os.stat()."""
return os.stat(filename).st_size
In case it isn't obvious, os.path.getsize()
comes from genericpath.py.
>>> os.path.getsize.__code__
<code object getsize at 0x1d457b0, file "/usr/lib/python2.7/genericpath.py", line 47>