Last answered:

13 Jul 2023

Posted on:

12 Jul 2023

0

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?

4 answers ( 1 marked as helpful)
Instructor
Posted on:

13 Jul 2023

0

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

Posted on:

13 Jul 2023

0

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? 


Instructor
Posted on:

13 Jul 2023

0

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

Posted on:

13 Jul 2023

0

Thank you for the response and clearing my doubt Hristina. Issue resolved.

Submit an answer