title: How to fix issues with comparing text of 2 files and comparing
body: <p>I have a script that gets the list of distribution switches from device manager "Master-EDR-List.txt".  It then grabs another txt file from a different server "New-EDR-List.txt".  Master list is pretty static until New list has additional EDRs that Master list is missing.</p>
  
  <p>I would like to compare these 2 files and save any EDR that is in New list but not in Master list.  I did write a compare script but it is not reliable.  I added some additional test EDRs in New list and I am getting unexpected results based on where I place them in the list.  I always get the new ones but sometimes I also get an EDR that is in both list and sometimes I get both new EDRs in same line with no spaces.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    old_lines = set((line.strip() for line in open('Master-EDR-List.txt', 'r+')))
      file_new = open('New-EDR-List.txt', 'r+')
      #file_diff = open('file_diff.txt', 'w')
  
      #Open Master File
      with open('Master-EDR-List.txt', 'r') as f:
          d = set(f.readlines())
  
      #Open New File
      with open('New-EDR-List.txt', 'r') as f:
          e = set(f.readlines())
  
      #Open Diff files to store differences
      open('file_diff.txt','w').close()
  
      with open('file_diff.txt', 'a') as f:
          for line in list(e - d):
              f.write(line)
  </code></pre>
  
  <p>Here are my lists I am using for testing:</p>
  
  <p>Master List:</p>
  
  <pre><code>rts41d-an28edr1.rt.tst.com
  rts41d-an28edr2.rt.tst.com
  rts41d-an32edr1.rt.tst.com
  rts41d-an32edr2.rt.tst.com
  rts41d-as19edr1.rt.tst.com
  rts41d-as19edr2.rt.tst.com
  rts41d-as21edr1.rt.tst.com
  rts41d-as21edr2.rt.tst.com
  rts12a-ah46edr2.rt.tst.com
  rts12a-al46edr2.rt.tst.com
  rts12a-as46edr1.rt.tst.com
  rts12a-as46edr2.rt.tst.com
  rts12a-as46edr2.rt.tst.com
  rts12a-aw46edr1.rt.tst.com
  rts12a-aw46edr2.rt.tst.com
  rts12a-aw46edr2.rt.tst.com
  rts12b-as46edr1.rt.tst.com
  rts12b-ax46edr1.rt.tst.com
  </code></pre>
  
  <p>New List:</p>
  
  <pre><code>rts41d-an28edr1.rt.tst.com
  rts41d-an28edr2.rt.tst.com
  rts41d-an32edr1.rt.tst.com
  rts41d-an32edr2.rt.tst.com
  rts41d-as19edr1.rt.tst.com
  rts41d-as19edr2.rt.tst.com
  rt511-sps5.rt.tst.com
  rts41d-as21edr1.rt.tst.com
  rts41d-as21edr2.rt.tst.com
  rts12a-ah46edr2.rt.tst.com
  rts12a-al46edr2.rt.tst.com
  rts12a-as46edr1.rt.tst.com
  rts12a-as46edr2.rt.tst.com
  rt511-sps6.rt.tst.com
  rts12a-as46edr2.rt.tst.com
  rts12a-aw46edr1.rt.tst.com
  rts12a-aw46edr2.rt.tst.com
  rts12a-aw46edr2.rt.tst.com
  rts12b-as46edr1.rt.tst.com
  rts12b-ax46edr1.rt.tst.com
  rt511-sps7.rt.tst.com
  </code></pre>
  
  <p>I added 2 test rt511-sps5, 6 and 7 in the list and instead of only getting these 3 item, I am getting this in my Diff file:</p>
  
  <p>Diff File:</p>
  
  <pre><code>rt511-sps7.rt.tst.comrt511-sps5.rt.tst.com
  rt511-sps6.rt.tst.com
  rts12b-ax46edr1.rt.tst.com
  </code></pre>
  
  <p>As you can see, sps7 and 5 are in same line for some reason and "rts12b-ax46edr1" should not be there as it is already in both files.</p>
  
  <p>Does anyone know why this is happening and how I can fix it?  New List can discover any new distribution switch and put it anywhere on list depending on its name.  I would like this script to only print out any new EDR in the list that Master does not have.</p>
  
  <p>Thanks</p>