2016-02-18 111 views
0

我试图创建一个非常基本的小操作系统,只是为了好玩,使用Python。到目前为止,我有这样的:如何将输入保存到文件,然后检查文件中的输入?

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() 

但我想程序运行,这样它会创建的用户名保存到一个文件,这样你就不必每次运行时创建一个新的用户名。有小费吗? (请不要评判我对我的“文本编辑器。”这就是那里,以便有一个目的,在签订)。

回答

0

你可以写一个文件这样

with open('myfile.txt', 'w') as f: 
    f.write(rUsername) 

一个简单的程序询问他们的用户名和检查用户,如果它是在一个文件中,如果它不存在,它写到自己的名字到文件中,从而创建一个新用户,用这个逻辑,你应该用自己的方式

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 
1

你可以创建一个用户名和相应密码的字典,然后将其保存到json文件中。

假设你dictinoary是类型

user_dict = {rUsername : rPassword} 

保存到文件“user.json”即。写操作

import json 
with open("users.json", "w") as f: 
    json.dumps(user_dict,f) 

读操作

import json 
with open("users.json", "r") as f: 
    user_dict = json.loads(f) 
0

以下是创建用户和用户检查中存在的系统或没有的功能。

我们使用Pickle库来将用户详细信息存储在字典结构中。

演示代码

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() 

  1. 的raw_input()被在Python 2.x的使用
  2. 输入中在Python 3.x的使用

几个环节

  1. File modes to read and write
  2. Pickling and Unpicking
相关问题