0

I have Python 3.5 code that logs a user.

It creates a user and logs them in.

After I kill the program and re-run it, it does not remember the details of the user it created.

How do I link a file to the document?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
  • 2
    Please share some of the code you have attempted, you are much more likely to get help here if you show you have done some work towards the problem first. – cfreear Nov 11 '16 at 12:04

2 Answers2

0

I was doing a similar project earlier this week and this is what i cane up with!

import csv

def displayMenu():
    status = input("Are you a registered user? y/n? ")  
    if status == "y":
        login()
    elif status == "n":
        newUser()

def login():

    with open('users.txt') as csvfile:
        reader = csv.DictReader(csvfile)
        database = []
        for row in reader:
            database.append(dict(username=row['username'],
                                 password=row['password'],
                                 function=row['function']))

loggedin = False
while not loggedin:
    Username = input('Fill in your username: ')
    Password = input('Fill in your password: ')
    for row in database:
        Username_File = row['username']
        Password_File = row['password']
        Function_File = row['function']
        if (Username_File == Username and
            Password_File == Password and
                Function_File == 'user'):
            loggedin = True
            print('Succesfully logged in as ' + Username)
        elif (Username_File == Username and
              Password_File == Password and
                Function_File == 'admin'):
            loggedin = True
            print('Succesfully logged in as the admin.')
        if loggedin is not True:
            print ('Failed to sign in, wrong username or password.')

def newUser():
    signUp = False
    while not signUp:
        NewUsername = input("Create login name: ")
        NewUsername = str(NewUsername)
        with open('users.txt', 'r') as UserData:
                reader = csv.reader(UserData, delimiter=',')
        if (NewUsername) in open('users.txt').read():
            print ('Login name already exist!')

        else:
            NewPassword = input("Create password: ")
            NewPassword = str(NewPassword)
            with open('users.txt', 'a') as f:
                writer = csv.writer(f)
                UserPassFunction = (NewUsername,NewPassword,'user')
                writer.writerows([UserPassFunction])
            print("User created!")
            signUp = True






# ---- Main ---- #

displayMenu()
Charlie
  • 15
  • 4
0

You can read and write data to a file to store the users login details, where Logindetails.txt is a .txt file stored in the same location as your program. Windows notepad uses .txt files.

import linecache
with open("LoginDetails.txt", "r") as po:
    LoginDetails = linecache.getline('LoginDetails.txt', int(LineNumber)).strip()

"r" will open the file in read only mode. "r+" will allow you to read and write to the file.

def replace_line(file_name, line_num, text):
    lines = open(file_name, 'r').readlines()
    lines[line_num] = text
    with open(file_name, 'w') as out:
        out.writelines(lines)

P.S I did not create this, original post here Editing specific line in text file in python

I would recommend using a SQL database for login details though, if you wish to use SQL I would recommend using the sqlite3 library/module

Also, your program is probally not able to 'remember my details' because you probally store the details as a variable which is changed by a user input, which is stored in RAM. This data is wiped once the program is closed, so if it is changed via a user input, the input will have to be re changed every time you close the program.

You can also scan the file for a string, such as a username. Search for string in txt file Python

Community
  • 1
  • 1
Mathew Wright
  • 15
  • 1
  • 7