Python Forum
how to write/overwrite data in a txt. file according to inp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to write/overwrite data in a txt. file according to inp
#1
so im like, really new to python and i wanna do this thing where it asks for an input (ex. response = input("")) and i want to make it so that when the input is written, it writes the response into an already existing txt file.

is there awy to do that?

any help is appreciated!
-Quinn Dance
Reply
#2
Somethin like this,if take away loop it will write one line.
filename = "file.txt"
print("Enter lines to save. Type Q to quit.")
# Append to an existing text file (or create it doesn't exist)
with open(filename, "a", encoding="utf-8") as f:
    while True:
        line = input("> ")
        if line.strip().upper() == "Q":
            break
        f.write(line + "\n")
Reply
#3
it works, thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Renumber page numbers and write result to file? Winfried 0 33 Nov-20-2026, 10:15 AM
Last Post: Winfried
  If I open a file write or append, is the file loaded into RAM? Pedroski55 11 1,147 Jan-14-2026, 07:49 AM
Last Post: Pedroski55
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 13 36,787 May-20-2025, 12:26 PM
Last Post: hanmen9527
  How to write variable in a python file then import it in another python file? tatahuft 4 2,288 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Write json data to csv Olive 6 2,477 Oct-22-2024, 06:59 AM
Last Post: Olive
  [SOLVED] [Linux] Write file and change owner? Winfried 6 3,263 Oct-17-2024, 01:15 AM
Last Post: Winfried
  What does .flush do? How can I change this to write to the file? Pedroski55 3 2,287 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 2,892 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 8,724 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 5,910 Nov-09-2023, 10:56 AM
Last Post: mg24

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020