Skip to content
Advertisement

Get the lines of txt file which not the first part or not exist in another file’s lines in python

I have two txt files and need to get the output in a new txt file :

one file has the below (named f.txt):

interface Eth-Trunk50
interface Eth-Trunk51
interface Eth-Trunk60
interface Eth-Trunk60.2535
interface Eth-Trunk100
interface GigabitEthernet0/0/0
interface GigabitEthernet1/1/1
interface GigabitEthernet1/1/4
interface GigabitEthernet1/1/10

and another file has the below lines (named x.txt):

interface Eth-Trunk50.1000
interface Eth-Trunk50.1030
interface Eth-Trunk51.2071
interface Eth-Trunk51.2072
interface Eth-Trunk100.108
interface Eth-Trunk100.109
interface Eth-Trunk100.111
interface GigabitEthernet1/1/0
interface GigabitEthernet1/1/1.660
interface GigabitEthernet1/1/1.662
interface GigabitEthernet1/1/2
interface GigabitEthernet1/1/10.3400
interface GigabitEthernet1/1/10.3600
interface GigabitEthernet1/1/10.3800
interface GigabitEthernet2/1/1
interface GigabitEthernet2/1/1.3010

i need to get the lines of f.txt file which not the first part or not exist in x.txt lines to get the output like below :

interface Eth-Trunk60
interface Eth-Trunk60.2535
interface GigabitEthernet0/0/0
interface GigabitEthernet1/1/4

and here is my python code but its not working for me , so thanks to help please .

def readB(Devices):
    nums = []
    with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default33.txt') as f:
        nums = f.read().split('n')
        nums = [n.strip() for n in nums if n !='']
    with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default55.txt') as x , open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/' + Devices + '-NO trust upstream default66.txt','w') as new :
        for n in nums:
          for line in x :
              if not line.startswith(n) :
                  print (n)
                  new.write(n+'n')
                  break

readB('10.0.130.13')

Advertisement

Answer

  1. read all line from both files

  2. remove the last 'n' of each line

  3. check whether the line is valid, use a valid flag here :

  • initialize valide flag as ‘True’

  • if any line is start with the checking line, mark the valid flag as ‘False’

  • filter lines by the valid flag

  1. write the result to the result file
def readB(Devices):
    fLines = open('./f.txt').readlines()
    xLines = open('./x.txt').readlines()
    with open('./result.txt', 'w') as new:
        for fLine in fLines:
            fLine = fLine.replace('n', '')

            valid = True
            for xLine in xLines:
                xLine = xLine.replace('n', '')
                if xLine.startswith(fLine):
                    valid = False
                    break
            if valid:
                print(fLine)
                new.write(fLine + 'n')


readB('10.0.130.13')
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement