current position:Home>First clear the content in the text, and then add content in the?

First clear the content in the text, and then add content in the?

2022-02-02 22:40:29 CSDN Q & A

Text text in , There are some data ;
Now I want to clear it first text Data in text , Then put the data obtained by the following code i, Add to text

l=[1,2,3]for i in l :    print(i)    fp=open('D:/text.txt','a+')    print(i,file=fp)    fp.close()



Refer to the answer 1:
l=[1,2,3]for i in l :    print(i)    fp=open('D:/text.txt','w')    print(i,file=fp)    fp.close()



Refer to the answer 2:



Refer to the answer 3:

Direct use w Not to go , It's not advisable for you to open and close files so often




Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022240271972.html

Random recommended