Entry
How can I get the size of a file in python?
Sep 13th, 2000 07:20
unknown unknown, Steve Holden, Robert W. Bill
Problem:
i've done:
self.logfile = open(filename, 'a')
and i'd like to do this next:
len_log = self.logfile.size()
...is there something comparable?
Solution:
Indeed there is. You can do:
import os
len_log = os.stat(filename)[6]
This functionality is available on both UNIX and Windows. To be more
nicely symbolic you could extend the example to:
import os
from stat import *
len_log = os.stat(filename)[ST_SIZE]
which makes it a bit more obvious what's going on.
--------------
However, because you are opening self.logfile for appending,
you can use:
self.logfile = open(filename, "a")
len_log = self.logfile.tell()