1

I am working on a program that asks the user to enter a username and password and the program checks if the username and password is in the text file. If the username and password is not in the text file, it asks if the user wants to create a new user. If the username and password matches one in the text file, it rejects the user input. If it is successful, the username and password is saved to the text file, on a new line (the username and password separated by a comma).

text.txt :

 John, Password
 Mary, 12345
 Bob, myPassword

Usercheck.py :

input: John
# Checks if 'John' in text.txt
input2: Password
# Checks if 'Password' in text.txt
output: Hello John!  # If both 'John' and 'Password' in text.txt


input: Amy
# Checks if 'Amy' in text.txt
input2: passWoRD
# Checks if 'passWoRD' in text.txt
output: User does not exist! # If 'Amy' and 'passWoRD' not in text.txt

output2: Would you like to create a new user?
# If 'yes'
output3: What will be your username?
input3: Amy
# Checks if 'Amy' in text.txt
output4: What will be your password?
input4: passWoRD
# Adds 'Amy, passWoRD' to a new line in text.txt

How could I check the text file text.txt for a username AND password that is separated by a ',' without the user entering a ','? And also be able to create a new username and password (which is separated by a ','), adding it to the text file?

4
  • Split the file's lines with a , delimiter. Commented Oct 7, 2016 at 21:45
  • This is a quite easy problem. Read on the web how to read a file in Python, how to read line by line, how to split strings, how to append a line to a file, etc. Commented Oct 7, 2016 at 21:45
  • 1
    ..or wait for someone to come along, take all the work out of doing that for you, and post a complete solution, as usual.. -_- Commented Oct 7, 2016 at 21:49
  • And Bounty, Bounty! Commented Oct 7, 2016 at 21:59

1 Answer 1

1

You may know the open() function. With this function you can open a file like this:

open('text.txt', 'a')

Parameter 1 is the file and parameter 2 is the mode (r for read only, w for write only and a for both and appending)

So to read the open file line by line :

file = open('text.txt', 'a')
lines = file.readlines()
for line in lines:
    name, pass = line.split(',')
    if name == 'whatever':
    #...

And finally to write to the file you've got the write() function.

file.write(name + ',' + pass)

I think that will help you to complet your programme. :)

Sign up to request clarification or add additional context in comments.

3 Comments

A opens the file in append mode, r+ opens the file in read and write mode.
Yeah, right... but AFAIK it's possible to read the file in a mode
Is this for python3? It gives me an error message when I try to run it : lines = file.lines() AttributeError: '_io.TextIOWrapper' object has no attribute 'lines'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.