Skip to content
Advertisement

Size of an open file object

Is there a way to find the size of a file object that is currently open?

Specifically, I am working with the tarfile module to create tarfiles, but I don’t want my tarfile to exceed a certain size. As far as I know, tarfile objects are file-like objects, so I imagine a generic solution would work.

Advertisement

Answer

JavaScript

Adding ChrisJY’s idea to the example

JavaScript

Note: Based on the comments, f.seek(0, os.SEEK_END) is must before calling f.tell(), without which it would return a size of 0. The reason is that f.seek(0, os.SEEK_END) moves the file object’s position to the end of the file.

Advertisement