擅长:python、mysql、java
<p>您可以关闭该文件,然后仅打开该文件进行写入,这将在写入新内容之前清除原始文件的内容:</p>
<pre class="lang-py prettyprint-override"><code>def removeUserFunc(SID):
# open file for reading
with open('Credentials.json','r') as json_file:
# read the data to variable:
data = json.load(json_file)
# open file for writing:
with open('Credentials.json','w') as json_file:
data['Credentials'][:] = [item for item in data['Credentials'] if item['username'] != SID.get()]
json.dump(data,json_file,indent=3,sort_keys=True)
</code></pre>