How to delete the contents of a file before writing into it in a python script?

前端 未结 2 874
时光说笑
时光说笑 2021-01-20 18:49

I have a file called fName.txt in a directory. Running the following python snippet (coming from a simulation) would add 6 numbers into 3 rows and 2 columns into the text fi

2条回答
  •  攒了一身酷
    2021-01-20 19:11

    You are using 'append' mode ('a') to open your file. When this mode is specified, new text is appended to the existing file content. You're looking for the 'write' mode, that is open(filename, 'w'). This will override the file contents every time you open it.

提交回复
热议问题