Python清空文本内容的两种方法

方法一:

file = open("test.txt", 'w').close()

直接清空“test.txt”里面的内容

方法二:

with open("test.txt", 'r+') as file:

  file.truncate(0)