擅长:python、mysql、java
<p>所以-我知道你已经做到了,但是你的<code>file.truncate()</code>和<code>file.truncate()</code>有点像黑魔法ey,没有太多使用,这会使你的代码不那么地道,更难维护</p>
<p>我认为,如果使用单独的文件句柄来读取和写入文件,这将更干净</p>
<pre><code>filename = './file.json'
with open(filename) as fp:
jsonstuff = json.load(fp)
jsonstuff['key'] = 'tes'
with open(filename, "w") as fp:
json.dump(jsonstuff, fp, indent=4)
</code></pre>