Reputation: 149
I'm trying to create a VERY basic little operating system just for fun, using Python. So far I have this:
rUsername = input("Create a new user: ")
rPassword = input("Create a password for the user: ")
tUsername = input("Username: ")
def username():
if tUsername != rUsername:
print("User not found.")
blank = input("")
exit()
username()
def password():
if tPassword != rPassword:
print("Incorrect password.")
blank = input("")
exit()
tPassword = input("Password: ")
password()
def typer():
typerCMD = input("")
print ("Hello, and welcome to your new operating system. Type 'help' to get started.")
shell = input("--")
if shell == ("help"):
print("Use the 'leave' command to shut down the system. Use the 'type' command to start a text editor.")
shell2 = input ("--")
if shell2 == ("leave"):
print("Shutting down...")
exit()
if shell2 == ("type"):
typer()
But I want the program to run so that it will save the created username to a file so you don't have to create a new username every time you run it. Any tips? (And please don't judge me on my "text editor." That's just there so that there's a purpose to signing in.)
Upvotes: 0
Views: 118
Reputation: 10223
Following are function of Create User and check user present in system or not.
We are used Pickle
library to store user details in dictionary structure.
Demo Code:
import os
import pickle
user_details_file = "user_details1.txt"
def createNewUser():
"""
Create New USer.
1. Accept USer name and PAssword from the User.
2. Save USer Name and Password to File.
"""
#- Check Login Detail file is present or not.
if os.path.isfile(user_details_file):
#- Load file into dictionary format.
fp = open(user_details_file, "rb")
data = pickle.load(fp)
fp.close()
else:
#- Set empty dictionary when user details file is not found.
data = {}
while 1:
username = raw_input("Enter Username:").strip()
if username in data:
print "User already exist in system. try again"
continue
password = raw_input("Enter password:")
data[username] = password
#- Save New user details in a file.
fp = open(user_details_file, "wb")
pickle.dump(data, fp)
fp.close()
return True
def loginUSer():
"""
Login User.
1. Open User Name and Password file.
2. Accept User name and Password from the User.
3. check User is present or not
"""
#- Check Login Detail file is present or not.
if os.path.isfile(user_details_file):
fp = open(user_details_file, "rb")
data = pickle.load(fp)
fp.close()
else:
#- Load file into dictionary format.
# We can return False from here also but why to tell user that your are first user of this application.
data = {}
username = raw_input("Enter Username:").strip()
password = raw_input("Enter password:").strip()
if username in data and password==data["username"]:
print "You login"
return True
else:
print "No user worong username or password"
return False
if __name__=="__main__":
new_userflag = raw_input("you want to create new user? yes/no").lower()
if new_userflag=="yes":
createNewUser()
loginUSer()
Note:
Few links:
Upvotes: 0
Reputation: 9634
You can write to a file like this
with open('myfile.txt', 'w') as f:
f.write(rUsername)
a simple program that asks the user for their username and checks if it's in a file, if it's not present it writes their name to the file, thereby creating a new user, with this logic you should be on your way
while True:
username = input('please enter your username or exit to quit the program')
if username == 'exit':
break
else:
with open('myfile.txt', 'r') as f:
for line in f:
print(line)
if username == line:
print('you belong here')
break
else:
with open('myfile.txt', 'a') as f:
f.write(username)
print('You dont belong but your name has been saved')
break
Upvotes: 0
Reputation: 2344
You can create a dictionary of of username and corresponding password, and then save it to json file.
Suppose your dictinoary is of the type
user_dict = {rUsername : rPassword}
Save into the file "user.json" ie. Write operations
import json
with open("users.json", "w") as f:
json.dumps(user_dict,f)
Read Operations
import json
with open("users.json", "r") as f:
user_dict = json.loads(f)
Upvotes: 1