Ask you a question and hope to get an answer

CSDN Q & A 2022-05-15 04:51:56 阅读数:627


With all of you , excuse me , Let me ask a little question , That is to say Python In the open TXT After the documents , Can you modify the contents ?( Be careful : Not add content , Is modification ) Or create a new text document TXT? Let's say I have a TXT, The content is “abcd” I want to change it to “bcdefg” Is that OK ? How to change ? I hope to get your reply , thank you !

Take the answer :

Read first txt Content to python in , Then treat it as a string , Modify the contents , Copy back .
with open(' File path and file name ', 'w') as f
f.write(' The revised content ')
If you don't have this file name , Will automatically create a txt file .

版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。