0

I tried to solve it in many different ways but I never get it to work properly. My problem is that I have a list of users that it is a TXT file and I want to remove one of the users.

So, the closes that I got is this:

elif name.lower() == 'remove':
        rem = input("Insert user name to remove \n ...")
        with open('names.txt', 'r+') as f:
            f.delete(f.find(rem))

The text document is something like this:

Alex
Sarah
Mathew
Sophie

Only one string (user) every line.

Full code for better understanding:

snames = list()
f_n = (open('names.txt')).read()
print("Welcome to NAME.app")
#try:
while True:
    name = input("\n - Insert name to logg in \n - ADD to save new user \n - LIST to see saved users \n - REMOVE to delete a user \n ...")
    if name.lower() == "add":
        n_input = input("Name:")
        with open('names.txt', 'a') as f:
            f.write(n_input + '\n')
            f.close()
        continue

    elif name.lower() == "list":
        with open('names.txt') as f:
            print(f.read().splitlines())

    elif name in f_n:
        print("Logged as", name.upper())
        nxt = input('Welcome, press enter to continue \n')
        if nxt == '':
            break

    elif name.lower() == 'remove':
        rem = input("Insert user name to remove \n ...")
        with open('names.txt', 'r+') as f:
            f.delete(f.find(rem))

    elif name.lower() == "exit":
        exit()
4
  • 3
    Does this answer your question? How to delete specific strings from a file? Commented Dec 1, 2020 at 18:21
  • 1
    Unless you are specifically asking about how to solve a cross-version compatibility problem (in which case your question should obviously describe that problem) you should not mix the python-2.7 and python-3.x tags. I have removed them both. Commented Dec 1, 2020 at 18:32
  • @tripleee Ok, sorry didn't know. Next time should I just python-3.x? Commented Dec 1, 2020 at 18:37
  • 1
    Probably yes, though these days Python 3 would be inferred if you don't say which version you are using. Commented Dec 1, 2020 at 18:39

1 Answer 1

1

Try this:

elif name.lower() == 'remove':
        rem = input("Insert user name to remove \n ...")
        with open('names.txt', 'r+') as f:
            l=f.readlines()
            l=[z for z in l if rem not in z]
        with open('names.txt', 'w') as f:
            f.writelines(l)

However, this solution will remove ALL occurencies of this name in the file. If there are duplicates and you want to remove some of them, not all, code must be adjusted

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

3 Comments

Traceback (most recent call last): File "C:\Users\aletr\Desktop\Python projects\Restaurant software\r_0.py", line 31, in <module> f.write(l) File "C:\Python30\lib\io.py", line 1487, in write s.__class__.__name__) TypeError: can't write list to text stream
I have updated the code, please try again
Jesus, I love you. Thank you very much!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.