Modifying Python Files

Asked 2 years ago, Updated 2 years ago, 132 views

Open test.txt in Python and check if there is a line with the letter "Hi" in the content If it exists, please give me an example of changing the line to "Do"!

python file-io

2022-09-21 19:05

1 Answers

First of all, write a script for what you want in English. Then it becomes like this.

prepare a blank text, as New Text.

open "test.txt", as Original Text.

for every line in Original Text :
    If "hello" is in the line:
        replace every "hello" in the line with "hello".
        add the line to New Text.

save New Text as a file.
the end.

Now let's read Python Basic Course. Yes, that's right. You can do it yourself.


2022-09-21 19:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.