You could seek to the end of the file, then call .tell()
:
import os
fileobj.seek(0, os.SEEK_END)
size = fileobj.tell()
fileobj.seek(0, os.SEEK_SET)
The above example uses the os.SEEK_*
constants to set how to interpret the seek offset (the first argument to .seek()
).
or you can use the os.fstat()
function to retrieve the size of your open file:
import os
size = os.fstat(fileobj.fileno()).st_size
The latter information can be more easily cached by the OS so will often be faster.