Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What’s the best way to get the file size? [closed]

There are actually three ways I have in mind to determine a files size:

  1. open and read it, and get the size of the string with len()
  2. using os.stat and getting it via st_size -> what should be the "right" way, because it's handled by the underlying OS
  3. os.path.getsize what should be the same as above

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.)


2 Answers

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

Edit:

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>
like image 137
gfortune Avatar answered Oct 29 '25 23:10

gfortune


Method 1 is the slowest way possible. Don't use it unless you will need the entire contents of the file as a string later.

Methods 2 and 3 are the fastest, since they don't even have to open the file.

Using f.seek(os.SEEK_END) and f.tell() requires opening the file, and might be a bit slower than 2&3 unless you're going to open the file anyway.

All methods will give the same result when no other program is writing to the file. If the file is in the middle of being modified when your code runs, seek+tell can sometimes give you a more up-to-date answer than 2&3.

like image 39
Baffe Boyois Avatar answered Oct 30 '25 01:10

Baffe Boyois