gpt4 book ai didi

python - 如何在 python 中获取 TemporaryFile 的大小?

转载 作者:行者123 更新时间:2023-12-01 19:35:17 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How can I check file size in Python?

(11 个回答)


3年前关闭。




目前,当我写一个临时:

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

关于python - 如何在 python 中获取 TemporaryFile 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com