目前,当我写一个临时文件时:
import tempfile
a = tempfile.TemporaryFile()
a.write(...)
# The only way I know to get the size
a.seek(0)
len(a.read())
有没有更好的办法?
import tempfile
a = tempfile.TemporaryFile()
a.write('abcd')
a.tell()
# 4
a.tell()
为您提供文件中的当前位置。如果您仅追加,则将是正确的。如果要通过搜索在文件周围跳跃,则首先使用以下命令搜索文件的末尾:a.seek(0, 2)
第二个参数“ whence” = 2表示位置相对于文件的末尾。https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句