pythonreadlines

Read content of txt files into lists to find duplicates


I'm new to Python.

My code should read 2 different .txt files into lists and compare them to find and delete duplicates.

Code

import os
dir = os.listdir
T = "Albums"
if T not in dir():
    os.mkdir("Albums")
with open('list.txt','w+') as f:
    linesA = f.readlines()
    print(linesA)   # output empty
with open('completed.txt','w+') as t:
    linesB = t.readlines()
    print(linesB)  # output empty
for i in linesA[:]:
    if i in linesB:
        linesA.remove(i)
print(linesA)
print(linesB)

I tried the code above with following inputs:

It should have first output the content of the lists, which were empty for some reasons.

Why are the read lists empty?


Solution

  • Does this help:

    Please note that if you want to use readlines you have to remove the new line for each entry.

    import os
    
    with open('list.txt','w+') as file:
        file.write("Foo\n")
        file.write("Bar")
    with open('completed.txt','w+') as file:
        file.write("Bar\n")
        file.write("Python")
    
    T = "Albums"
    if not os.path.exists(T):
        os.mkdir("Albums")
    with open('list.txt','r+') as f:
        linesA = f.read().split("\n")
        print(linesA)
    with open('completed.txt','r+') as t:
        linesB = t.read().split("\n")
        print(linesB)
    for entry in list(linesA):
        if entry in linesB:
            linesA.remove(entry)
    print(linesA)
    print(linesB)
    

    Output:

    ['Foo', 'Bar']
    ['Bar', 'Python']
    ['Foo']
    ['Bar', 'Python']