-1
How can I save an input from a user that is registered in my program so that even after it closes the program and opens it again it is possible to print only that user’s input? It is possible to do this using a . txt file?
-1
How can I save an input from a user that is registered in my program so that even after it closes the program and opens it again it is possible to print only that user’s input? It is possible to do this using a . txt file?
0
Yes it is possible. You can first check if the.txt file exists and if it has any content. To check if it exists just use the function os.path.exists(path)
.
After entering the block if
the program receives user input and writes this input to the.txt file. If you do not enter the if
because the file exists and has a content, the program will print what the user last typed. Example:
import os
fn = 'arquivo.txt'
if os.path.exists(fn):
with open(fn) as file:
text = file.read()
else:
text = None
if not text:
with open(fn,'w') as file:
text = input("Digite alguma coisa: ")
file.write(text)
else:
print("Você já tinha digitado:",text)
Browser other questions tagged python python-3.x
You are not signed in. Login or sign up in order to post.
What would be "user" in this context? The operating system’s own user running your program or someone else? If it is the second option, how will identify a user?
– Woss
Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of this. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English.
– Maniero