In [4]:
filename = "WMC1-Message.txt"
In [5]:
# Let's write to the file!
message = input("Type a message and I'll write it to %s:" % filename)
with open (filename,'w') as file_out:
file_out.write(message)
print("Done!")
In [6]:
with open(filename, 'r') as file_in:
message = file_in.read()
print("Here's the message in %s: %s" % (filename, message))
NOTE: Show this is really persistent.
In [8]:
def put_text_in_file(text):
with open("test-messages.txt", 'a', encoding='utf-8') as dog:
dog.write(text + "\n")
def get_text_out_file():
with open("test-messages.txt", 'r') as file:
contents = []
for line in file:
contents.append(line.strip())
return contents
while True:
user_input = input("Please enter a message (Enter to view all messages)")
if user_input == '':
break
put_text_in_file(user_input)
messages = get_text_out_file()
print(", ".join(messages))
In [ ]: