在 Python 中将 JSON 数据写入文件后是否需要关闭文件

Mus*_*ger 2 python json file python-2.7

如果我使用:

with open(Name, 'w') as outfile:
    json.dump(json_object, outfile)
Run Code Online (Sandbox Code Playgroud)

之后是否有必要使用 file.close() 方法?

在一般情况下,比如在 python 中将普通字符串写入文件,不使用 file.close() 方法的后果是什么?

tra*_*t0r 10

使用 'with' 构造的优点正是您不必担心在此处关闭文件,因为当文件超出范围时它会自动关闭。