Top Of Tech
Top Of Tech

Reputation: 301

How do you check for a line of text in text files?

I am receiving no errors, but it does not seem to check for the text in my file, as it will append anything you input. Can anyone help?

guest_name = input("What is your name: ")
with open("guests.txt", "a+") as guests_txt:

    names = guests_txt.readlines()


    def find_name():
        if guest_name.lower() in names:
            print("Your name is already taken.")
            name_taken = True
            return name_taken

    name_taken_function = find_name()

    if not name_taken_function:
        guests_txt.write(f"{guest_name.lower()}\n")

Upvotes: 3

Views: 541

Answers (5)

patmcb
patmcb

Reputation: 443

You can achieve this much more simply than a lot of these other answers.

guest_name = input("What is your name: ")
with open("guests.txt", "a+") as guest_txt:
    guest_txt.seek(0)
    names = guest_txt.read().splitlines()
    if guest_name.lower() in names:
        print(f"Sorry, {guest_name}, your name is already taken.")
    else:
        guest_txt.write(f"{guest_name.lower()}\n")
        print(f"Thanks, {guest_name}, your name has been recorded.")

Upvotes: 3

Splitter
Splitter

Reputation: 220

In order to append new name you can use guests_file.seek(0, io.SEEK_END). Also I would recommend to simplify the code and improve names:

guest_name = input("What is your name: ").lower()
with open("guests.txt", "a+") as guests_file:
    guests_file.seek(0)
    names = guests_file.readlines()

    def name_is_taken():
        if guest_name + '\n' in names:
            print("Your name is already taken.")
            return True

    if not name_is_taken():
        guests_file.write(f"{guest_name}\n")

Upvotes: 1

Red
Red

Reputation: 27567

The reason it doesn't work is because the .readlines() method doesn't strip away the extra '\n', and so, the name input by the user will never be equal to the existing ones. Use .read().splitlines() instead. Also, try opening the file twice, once in read mode, and then in append mode:

guest_name = input("What is your name: ")

with open("guests.txt", "r") as guests_txt:
    name_taken = False
    names = guests_txt.read().splitlines()
    if guest_name.lower() in names:
        if guest_name.lower() in names:
            print("Your name is already taken.")
            name_taken = True

with open("guests.txt", "a") as guests_txt:
    if not name_taken:
        guests_txt.write(f"\n{guest_name.lower()}")

Upvotes: 0

Cagri
Cagri

Reputation: 366

When you're opening a file with a+, position is set at the last character. When you try to read it, as there's nothing else after that last character, you will get an empty text.

Use guests_txt.seek(0) to seek to the beginning, and then read.
Another problem is, .readlines() won't strip \n character. Instead you can use .read().splitlines() to get the lines without newline character:

guest_name = input("What is your name: ")
with open("guests.txt", "a+") as guests_txt:

    guests_txt.seek(0)
    names = guests_txt.read().splitlines()

    def find_name():
        if guest_name.lower() in names:
            print("Your name is already taken.")
            name_taken = True
            return name_taken

    name_taken_function = find_name()

    if not name_taken_function:
        guests_txt.write(f"{guest_name.lower()}\n")

Upvotes: 2

augustomen
augustomen

Reputation: 9739

The mode a+ opens the file for append, so when you open it, the current position is at the end of the file. readlines() returns an empty list.

You should do guests_txt.seek(0) before reading it.

Upvotes: 6

Related Questions