Currently when I write to a temporary:
import tempfile
a = tempfile.TemporaryFile()
a.write(...)
# The only way I know to get the size
a.seek(0)
len(a.read())
Is there a better way?
Currently when I write to a temporary:
import tempfile
a = tempfile.TemporaryFile()
a.write(...)
# The only way I know to get the size
a.seek(0)
len(a.read())
Is there a better way?
import tempfile
f = tempfile.TemporaryFile()
f.write('abcd')
f.tell()
# 4
f.tell()
returns an integer giving the file object’s current position in the file represented as number of bytes from the beginning of the file when in binary mode and an opaque number when in text mode.
If you are jumping around the file with seek, then seek to the end of the file first with:
a.seek(0, 2)
the second parameter "whence"=2 means the position is relative to the end of the file.
https://docs.python.org/3.11/tutorial/inputoutput.html#methods-of-file-objects
You can use os.stat
assuming the file has either been closed or all pending writes have been flushed
os.stat(a.name).st_size