Python中清空文件内容的方法有两种:
with open("file.txt", "w") as file: file.truncate()
with open("file.txt", "w") as file: file.write("")
无论是使用truncate()方法还是write()方法清空文件内容,都需要以写入模式(“w”)打开文件。