Trying to get my code to read a text file for existing users

I am relatively new to coding and I am creating a simple login system for an assignment. I have created code so far that is able to register new users and save that data to the text file, but I am having a huge mind blank on what to do to get it to register existing users. So for example, when the user goes to register, if the username already exists it stops them from proceeding; as well as when they go to login, it recognises them in the system. Not sure if I am over thinkiung things but if someone could point me in the right direction I would very much appreciate it!!! Thank you!

import string
import time
import secrets

file = open('accounts.txt.txt', 'a+')

master = 'admin:password'

#random password generation
def generate_random_password(length=10, use_uppercase=True, use_lowercase=True, use_digits=True, use_punctuation=True):
character_sets = []

if use_uppercase:
    character_sets.append(string.ascii_uppercase)
if use_lowercase:
    character_sets.append(string.ascii_lowercase)
if use_digits:
    character_sets.append(string.digits)
if use_punctuation:
    character_sets.append(string.punctuation)

if not character_sets:
    raise ValueError("At least one character set must be selected.")

alphabet = ''.join(character_sets)
password = ''.join(secrets.choice(alphabet) for i in range(length))
return password


print("Welcome! Please choose one of the below options:")

#Home screen/menu
print("bA. Login")
print("bB. Register")
print("bC. View Accounts")
print("bD. Exit")
choice = input("Enter your choice (A/B/C/D): ").upper()

choice = choice.upper()#this should allow both lower and upper case letters

if choice == "A":
username = input("Please enter your username: ")
password = input("Please enter your password: ")
if username in file:#TBC - need to have code recognise username and password data in file
    print("Login Successful! Welcome " + username + "!")
else:
    print("Invalid username or password! Please try again.")

elif choice == "B":
 lst = []
 username = input("Please enter a username: ")
 find = read.file
 for lines in find:
    if username == lines[0]:
        print("Username already in use. Please create another.")
lst.append(username + ' ')
generate_new_password = input("Do you want to generate a random password? (yes/no): ").lower()

if generate_new_password == 'yes':
    if generate_new_password == "yes":
        # Password generation settings
        use_uppercase = input("Include uppercase letters? (yes/no): ").lower() == "yes"
        use_lowercase = input("Include lowercase letters? (yes/no): ").lower() == "yes"
        use_digits = input("Include digits? (yes/no): ").lower() == "yes"
        use_punctuation = input("Include punctuation? (yes/no): ").lower() == "yes"
        password_length = int(input("Enter password length (minimum 10): "))  # Minimum length requirement

        try:
            password = generate_random_password(
                length=password_length,
                use_uppercase=use_uppercase,
                use_lowercase=use_lowercase,
                use_digits=use_digits,
                use_punctuation=use_punctuation
            )
            print(f"Generated password: {password}")
        except ValueError as e:
            print(e)
#Password Creation______________________________________________Updates the text file only if password > 10
else:
    password = input("Create your password: ")
    if len(password) < 10:
        print("Password must be at least 10 characters long.")
    if len(password) >= 10:
        print("User details registered!")
        lst.append(password + 'n')
        file.writelines(lst)
        file.close()



elif choice == "C": #View Accounts menu for admin only
print("To view customer account information, please enter administration details below")
username = input("Please enter admin username: ")
password = input("Please enter admin password: ")
if username in master and password in master:
    print("Welcome " + username + "!")

    print(file.read())

elif choice == "D":#Exit program
print("Exiting program...")
time.sleep(2)#2 second time delay
print("Thank you! See you again soon!")
file.close()

else:
print("Invalid choice. Please try again.")

New contributor

Skye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật