gpt4 book ai didi

python - joblib 转储写入 0 字节文件

转载 作者:行者123 更新时间:2023-12-01 06:45:10 26 4
gpt4 key购买 nike

joblib.dump似乎对我没有任何作用。可能我遇到了一些版本冲突之类的问题。如有任何帮助,我们将不胜感激。

我的joblib版本:0.13.2
似乎也影响 0.14.0

重现:

import joblib
import os

foo = open("bar", "w+b")
joblib.dump("test", foo)
print(os.stat("bar").st_size)

#prints 0... expect the size of a file containing the pickled string "test" > 0 bytes

最佳答案

缓冲区尚未写入磁盘,因此文件已创建但没有内容。您必须刷新内部缓冲区,然后将内容写入文件:

>>> foo = open("bar", "w+b")
>>> joblib.dump("test", foo)
>>> foo.flush()
>>> print(os.stat("bar").st_size)
14

或者使用自动执行此操作的上下文管理器:

>>> with open("bar", "w+b") as foo:
>>> joblib.dump("test", foo)
>>> print(os.stat("bar").st_size)
14

或者您可以禁用缓冲:

>>> foo = open("bar", "w+b", buffering=0)
>>> joblib.dump("test", foo)
>>> print(os.stat("bar").st_size)
14

也可以看看这里:How often does python flush to a file?

关于python - joblib 转储写入 0 字节文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59257466/

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