xSabotagex
xSabotagex

Reputation: 93

how to detect already saved link in a text file

so i made this really simple program that saves the links i give it to a text file and i want it to tell me if i try to save a link that's already there but it won't! what am i doing wrong?

import os

space = " "

Links_lst = []

Links_i_saved = "Links i saved2.txt"

current_directory = os.listdir(os.getcwd())
if Links_i_saved in current_directory:
    with open(Links_i_saved, "r") as f:
        for i in f.readlines():
            if "Link: " in i:
                Links_lst.append(i.split("Link: ")[1])
            else:
                Links_lst.append(i)

while True:
    inpt = input("Give us a link (type 'break' to exit): ")
    if inpt == "break":
        break        
    else:
        if len(inpt) > 0:
            with open(Links_i_saved, "a+") as f:
                if inpt not in Links_lst:
                    if "." in inpt:
                        f.write("\n" + inpt.split(".", 2)[1] + space * (15 - len(inpt.split(".", 2)[1])) + space * 5 + "Link: " + inpt)
                    else:
                        f.write("\n" + inpt + space * (15 - len(inpt)) + space * 5 + "Link: " + inpt)
                    Links_lst.append(inpt)
                else:
                    print("You already saved this one!")
        else:
            print("No empty strings please!")

i want it to say "You already saved this one!" when the link already exists but it won't unless i try to save it twice with the program running (i don't want to use .read() because then it would "detect" www.google.com for example if i had any google URLs saved)

Upvotes: 0

Views: 37

Answers (1)

Parth Sarthi Sharma
Parth Sarthi Sharma

Reputation: 445

If you try to print already saved links, you will notice that the links have trailing whitespaces \r\n in front of them. These white spaces are causing this error.

Try this code:

import os

space = " "
Links_lst = []
Links_i_saved = "Links i saved2.txt"
current_directory = os.listdir(os.getcwd())

if Links_i_saved in current_directory:
    with open(Links_i_saved, "r") as f:
        for i in f.readlines():
            if "Link: " in i:
                Links_lst.append((i.split("Link: ")[1]).rstrip())
            else:
                Links_lst.append(i)

print(Links_lst)

while True:
    inpt = input("Give us a link (type 'break' to exit): ")
    if inpt == "break":
        break        
    else:
        if len(inpt) > 0:
            with open(Links_i_saved, "a+") as f:
                if inpt not in Links_lst:
                    if "." in inpt:
                        f.write("\n" + inpt.split(".", 2)[1] + space * (15 - len(inpt.split(".", 2)[1])) + space * 5 + "Link: " + inpt)
                    else:
                        f.write("\n" + inpt + space * (15 - len(inpt)) + space * 5 + "Link: " + inpt)
                    Links_lst.append(inpt)
                else:
                    print("You already saved this one!")
        else:
            print("No empty strings please!")

Upvotes: 1

Related Questions