简体   繁体   中英

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?

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)

Less code

with open("text.txt", "w") as file:
    file.write(input("Write on file> "))

Something reusable

def write_input(file):
    "Get and input and writes it to a file"
    inp = input("Write something: ")
    with open(file, "w") as file:
        file.write(inp)
    return inp


write_input("mytext.txt")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM