Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing Text in another file with a separate program

I have two files:

text

and

program.py

I insert text into my

text

file using the line:

inp=input('Text by the user')
with open("text.py", "a") as myfile:
   myfile.write(inp)

How can i make the program delete the line in text having it say:

text2

not

text1 text2

when run two times?

like image 925
Aliaksandr S. Avatar asked Jan 28 '26 06:01

Aliaksandr S.


1 Answers

Open in write mode (w) instead of append mode (a). This blanks the file before writing to it.

inp=input('Text by the user')
with open("text.py", "w") as myfile:
   myfile.write(inp)
like image 176
Joe Iddon Avatar answered Jan 30 '26 01:01

Joe Iddon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!