pythonlinuxfilefilesystemsext2

How do I determine an open file's size in Python?


There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't been written to disk yet?


Solution

  • You could start with something like this:

    class TrackedFile(file):
        def __init__(self, filename, mode):
            self.size = 0
            super(TrackedFile, self).__init__(filename, mode)
        def write(self, s):
            self.size += len(s)
            super(TrackedFile, self).write(s)
    

    Then you could use it like this:

    >>> f = TrackedFile('palindrome.txt', 'w')
    >>> f.size
    0
    >>> f.write('A man a plan a canal ')
    >>> f.size
    21
    >>> f.write('Panama')
    27
    

    Obviously, this implementation doesn't work if you aren't writing the file from scratch, but you could adapt your __init__ method to handle initial data. You might also need to override some other methods: writelines, for instance.

    This works regardless of encoding, as strings are just sequences of bytes.

    >>> f2 = TrackedFile('palindrome-latin1.txt', 'w')
    >>> f2.write(u'A man a plan a canál '.encode('latin1')
    >>> f3 = TrackedFile('palindrome-utf8.txt', 'w')
    >>> f3.write(u'A man a plan a canál '.encode('utf-8'))
    >>> f2.size
    21
    >>> f3.size
    22