Resolved: Just needed help in knowing how to edit a file already written and closed
How to edit the contents of the file already written and closed?
Hey Abhishek,
Thank you for reaching out!
Please, refer to the following thread which might answer your question:
https://365datascience.com/q/e9070b79e9
Let me know if something remains unclear.
Kind regards,
365 Hristina
Hey Hristina,
Thank you for your response. I understood the append function. I wanted to know if we can edit and change the names of the content already in the file. Like in the example you provided, can we edit the text Line 1 and Line 2?
Hey again,
Thank you for clarifying.
You can do this, for example, in the following way:
1. First, open (or create) a file in 'write' mode and write some text inside.
2. Then, open the file again, this time in read mode. Use the replace() function to first specify the text you want to replace (in this case, 'Line') and then the text that would replace it (in this case, 'Row'). The variable to_replace
is a string containing the updated text from the sample.txt file.
3. Finally, open the file in 'write' mode and write the content stored in the to_replace
variable.
Hope this helps!
Kind regards,
365 Hristina
Thank you for the response and clearing my doubt Hristina. Issue resolved.