1

ive been searching for ages for a solution on my problem: When a user types in a name, which is already saved in a .txt file, it should print "true". If the username is not already existing it should add the name, the user typed in. The Problem is, that it even prints out true when the typed name is "Julia", but "Julian" is already in the list. I hope you get my point. I already read maaany solutions here on stackoverflow but nothing worked for me when working with a .txt file My code:

import mmap
username = input("username: ")
names_file = open("names_file.txt", "a")

paste = bytes(username, 'utf-8')
with open("names_file.txt", "rb", 0) as file, \
     mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
    if s.find(paste) != -1:
        print("true")
    else:
        names_file.write("\n" + username)
        print(username + " got added to the list")



names_file.close()
Boritobison
  • 85
  • 1
  • 1
  • 7
  • 1
    Can you add a snippet of your text file? – Swetank Poddar May 13 '20 at 17:23
  • `s.find(paste)` will attempt to find the _substring_ `paste` in `s`. Your code will return True for `yeahJuliatueu` as well. If you want to search for the _word_ `Julia`, you need to search _among words_. – ForceBru May 13 '20 at 17:26
  • Does this answer your question? [checking if word exists in a text file python](https://stackoverflow.com/questions/44205923/checking-if-word-exists-in-a-text-file-python) – stovfl May 13 '20 at 18:04

3 Answers3

1
username = input("username: ")
found = False
with open("names_file.txt", "r") as file:
    for line in file:
        if line.rstrip() == username:
            print("true")
            found = True
            break
if not found:
    with open("names_file.txt", "a") as file:
        file.write( username + "\n")
        print(username + " got added to the list")
Błotosmętek
  • 12,717
  • 19
  • 29
1

You could add the newline after the name and search for the name with the newline character:

import mmap
username = input("username: ")
names_file = open("names_file.txt", "a")

paste = bytes('\n' + username + '\n', 'utf-8')
with open("names_file.txt", "rb", 0) as file, \
    mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
    if s.find(paste) != -1:
        print("true")
    else:
        names_file.write('\n' + username + '\n')
        print(username + " got added to the list")

names_file.close()

This will not work for names with spaces inside -- for such cases you'll have to define different separator (also if all names begin with a capital letter and there are no capital letters in the middle of the name then you could spare the newline before the name)

jerry
  • 499
  • 4
  • 10
0

Try this i have updated my answer.

import mmap
import re
status = False
username = input("username: ")
names_file = open("names_file.txt", "a")

paste = bytes(username, 'utf-8')
with open("names_file.txt", "rb", 0) as file, \
    mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
    for f in file:
        f = f.strip()
        if f == paste:
            print("true")
            status = True
    if status == False:
        names_file.write("\n" + username)
        print(username + " got added to the list")



names_file.close()