Home > front end >  username and password from csv instead of text file
username and password from csv instead of text file

Time:11-11

I have a text file to read and write usernames and passwords. I would like it to instead be on a csv to have 1. Sorted charting 2. Any words on the text file can be used for the username and password even if they are in separate lines, they can be used interchangeably, which was unintentional. I would like it to only use info from the correct row. Such as [apple, pear] [banana, orange]. It should only accept it if apple and pear or banana and orange are together. This code works fine it is just running from a blank text file.

import time
import sys

text1 = input("\n Write username ")
text2 = input("\n Write password ")

saveFile = open('usernames passwords', 'a')
saveFile.write(text1   '    '   text2   '\n')

saveFile = open('usernames passwords', 'r')
saveFile.seek(0)
uap = saveFile.read()

saveFile.close()

max_attempts = 3
attempts = 0

while True:
    print("Username")
    username = input("")

    print("Password")
    password = input("")

    if username in uap and password in uap:
        print("Access Granted")
    else:
        attempts =1
        if attempts >= max_attempts:
            print(f"reached max attempts of {attempts} ")
            sys.exit()
        print("Try Again (10 sec)")
        time.sleep(10)
        continue
    break

CodePudding user response:

All problem is because you use read() and you get all as single string and later it check password, username in full string.

You should use readline() to get every line as separated string - and later you need for-loop to check password, username in every line separatelly.

# ... code ...

save_file = open('usernames passwords', 'a')
save_file.write(text1   '   '   text2   '\n')
save_file.close()

read_file = open('usernames passwords', 'r')
all_lines = read_file.readlines()
read_file.close()

max_attempts = 3
attempts = 0

while True:
    print("Username")
    username = input("")

    print("Password")
    password = input("")
    
    found = False
    for line in all_lines:
        if username in line and password in line:
           found = True
           break  # no need to check other lines
        
    if found:
        print("Access Granted")
        break
    else:
        # ... code ...

but this gives access if you put password as username and username as password.

You would have to split every line to get list with [password, username] and next check it with indexex [0],[1], and use == instead of in to make sure that it not in some longer string - ie. "abc" in "xabcy" gives True but "abc" == "xabcy" gives False

# ... code ...

save_file = open('usernames passwords', 'a')
save_file.write(text1   '   '   text2   '\n')
save_file.close()

read_file = open('usernames passwords', 'r')
all_lines = read_file.readlines()
all_lines = [line.strip().split('   ') for line in all_lines]  # split values in lines
read_file.close()

max_attempts = 3
attempts = 0

while True:
   print("Username")
   username = input("")

   print("Password")
   password = input("")
   
   found = False
   for line in all_lines:
       if username == line[0] and password == line[0]:  # use indexes `[0]`, `[1]`
          found = True
          break  # no need to check other lines
       
   if found:
       print("Access Granted")
       break
   else:
       # ... code ...

Of course this way you almost have csv file - but with ' ' instead of , as separator. But it can be safer to use csv module for this. But as I remeber it can't use multichar separator so you will have to delete usernames passwords and create it again with new code - or you will have to manually replace in file separator ' ' with ,

import csv

# ... code ...

save_file = open('usernames passwords', 'a')
cvs_writer = csv.writer(save_file)
cvs_writer.writerow( [text1 , text2] )
save_file.close()

read_file = open('usernames passwords', 'r')
cvs_reader = csv.reader(read_file)
all_lines = list(cvs_reader)
read_file.close()

max_attempts = 3
attempts = 0

while True:
    print("Username")
    username = input("")

    print("Password")
    password = input("")
    
    found = False
    for line in all_lines:
        if username == line[0] and password == line[1]:
           found = True
           break
        
    if found:
        print("Access Granted")
        break
    else:
        # ... code ...
  • Related