Reputation: 761
I am using python to make a simple info retrieving program that gets a clientID and a client id for a user is the first 5 characters of every line the txt file looks like this
RYMAU
ELOWE
RUKUM
KLKID
LAJON
I want for a user to enter a 5 letter id and check through each line for one of those specific client ids at the moment i have come up with this :
clientID = input('Enter your clientID : ')
fob = open('clientRecords.txt', 'r') # opens file stores it in a var called fob and reads from it
if (clientID in fob.readline(5)):
print('Access granted')
else:
print('Access denied')
fob.close()
But this only checks the first 5 letters of the first line not all of them.
This is how the text file actually looks:
NeQua,High,Running,5,Swimming,40,Aerobics,40,Football,20,Tennis,10
ImKol,Moderate,Walking,40,Hiking,0,Cleaning,40,Skateboarding,30,Basketball,20
YoTri,Moderate,Walking,20,Hiking,30,Cleaning,40,Skateboarding,20,Basketball,40
RoDen,High,Running,20,Swimming,20,Aerobics,40,Football,30,Tennis,50
NaThe,Moderate,Walking,30,Hiking,30,Cleaning,20,Skateboarding,10,Basketball,30
ReWes,Moderate,Walking,30,Hiking,20,Cleaning,50,Skateboarding,40,Basketball,20
BrFre,High,Running,20,Swimming,30,Aerobics,30,Football,30,Tennis,20
KaDat,High,Running,30,Swimming,20,Aerobics,10,Football,20,Tennis,30
ViRil,High,Running,50,Swimming,50,Aerobics,60,Football,40,Tennis,50
TrGeo,High,Running,10,Swimming,20,Aerobics,30,Football,30,Tennis,20
DaWay,High,Running,60,Swimming,50,Aerobics,40,Football,50,Tennis,50
CaAma,High,Running,30,Swimming,20,Aerobics,10,Football,20,Tennis,30
ArRes,High,Running,0,Swimming,10,Aerobics,30,Football,20,Tennis,20
BeVic,High,Running,20,Swimming,20,Aerobics,30,Football,30,Tennis,10
MaFre,High,Running,10,Swimming,20,Aerobics,20,Football,40,Tennis,30
AnMer,Moderate,Walking,40,Hiking,30,Cleaning,30,Skateboarding,20,Basketball,30
SaBro,Moderate,Walking,30,Hiking,20,Cleaning,30,Skateboarding,20,Basketball,20
PoLig,Moderate,Walking,20,Hiking,20,Cleaning,20,Skateboarding,30,Basketball,30
HeZbe,Moderate,Walking,30,Hiking,40,Cleaning,20,Skateboarding,40,Basketball,10
GiLop,Moderate,Walking,40,Hiking,0,Cleaning,30,Skateboarding,40,Basketball,20
DeTur,Moderate,Walking,10,Hiking,0,Cleaning,10,Skateboarding,30,Basketball,30
LaKin,Moderate,Walking,20,Hiking,20,Cleaning,30,Skateboarding,30,Basketball,20
AnVen,Moderate,Walking,50,Hiking,0,Cleaning,50,Skateboarding,50,Basketball,20
LoLew,Moderate,Walking,10,Hiking,20,Cleaning,10,Skateboarding,30,Basketball,40
NyRed,Moderate,Walking,0,Hiking,0,Cleaning,0,Skateboarding,0,Basketball,20
Upvotes: 0
Views: 1575
Reputation: 155363
You need to read all the lines, and test the first five characters of each line:
with open('clientRecords.txt') as fob:
allowed_access = any(line.startswith(clientID) for line in fob)
if allowed_access:
print('Access granted')
else:
print('Access denied')
allowed_access
will be True
if any line in the file starts with the provided prefix, and it will stop checking lines as soon as it finds a hit.
Note: To avoid cheating, you probably want to verify that the provided clientID
is actually five characters long.
If you want to get the line associated with that ID, not just whether or not the ID exists, you can modify that check to:
with open('clientRecords.txt') as fob:
access_data = next((line for line in fob if line.startswith(clientID)), None)
if access_data is not None: # clientID exists, and access_data is the line it was found on
Also, if you'll be performing this test a lot, and the records file rarely changes, you may want to cache in memory to reduce I/O work. You'd load the valid IDs in once up front (not in any loop or event handler that validates users):
with open('clientRecords.txt') as fob:
known_users = {line[:5] for line in fob}
Then your per login test simplifies to:
if clientID in known_users:
print('Access granted')
else:
print('Access denied')
which will run (for all intents and purposes) instantly, vs. the meaningful cost in reopening and rereading the file on each test. You'd probably want a staleness timeout after which you'd recache the known_users
, or os.stat
the file every once in a while and recache if it's modified.
The version that gets the associated line is:
with open('clientRecords.txt') as fob:
known_users = {line[:5]: line for line in fob}
and to check/lookup:
access_data = known_users.get(clientID) # Returns None if the clientID is not found, or the complete line if it is found
if access_data is not None:
print('Access granted')
else:
print('Access denied')
Upvotes: 7