0
def main():
    word = input("Enter word to search: ")
    list = open('lol.txt', 'r', encoding='latin-1')
    lines = list.readlines()
    list.close()
    
    flag = 0 
    
    for line in lines:
        line = line.strip()
        if line.find(word) == -1:
            pass
        else:
            print(f"{line} is here sir...")
            flag += 1
            
     if flag == 0:
        list = open('lol.txt', 'w', encoding='latin-1')
        list.write(word)
        list.close()

main()

Im just trying to insert a given word into the wordlist but it keeps on overwriting the wordlist.. how would I go about append the word to the end ?

  • 4
    Change the `'w'` in your `open()` call to `'a'` (from **write** to **append**). – Samwise Mar 05 '22 at 15:46
  • As the above implies: It's not the `write()` call that's deleting old contents, it's the `open()`, which is happening with `O_TRUNC` when `w` is used, but instead has `O_APPEND` when passed `a`. – Charles Duffy Mar 05 '22 at 15:54

0 Answers0