I am using Python 2.7 on Windows, and I am very new to Python so forgive me if this is an easy one.
Everything that I have read says that tell() returns the "position", which I believe is basically the cursor position that we are currently at in the read. OK, that sounds helpful, but I cannot figure out how to find the total "positions" of the file to calculate a percentage.
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.