How would I go about saving multiple lines of text without stretching out my code?
It's hard to put in to words but I'll give an example of what I've done.
wri_name=input("What do you want to call your new file? (Extension included): ")
print("Type what you want your file to contain!: ")
wri_con1=input("1> ")
with open((wri_name), 'w') as f:
f.writelines(["\n" wri_con1])
I'm trying to make an improvement by making a text-box that is seemingly endless but can still be saved into a file. Take Microsoft Notepad as an example. I'm trying to replicate a program similar to that but I've really been scratching my head about what I could do here.
CodePudding user response:
When you write in loop then you have to open in append mode
- open(..., "a")
- because write mode
removes previous content.
filename = input("What do you want to call your new file? (Extension included): ")
print("Type what you want your file to contain!: ")
while True:
text = input("1> ")
if text.lower() == "quit":
break
with open(filename, 'a') as f:
f.write(text "\n")
Or you have to open it only once
filename = input("What do you want to call your new file? (Extension included): ")
print("Type what you want your file to contain!: ")
# --- before loop ---
f = open(filename, 'w')
# --- loop ---
while True:
text = input("1> ")
if text.lower() == "quit":
break
f.write(text "\n")
# --- after loop ---
f.close()