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

  python, txt

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')

Source: Python Questions

LEAVE A COMMENT