Home > Back-end >  Pass Variable from function to another python
Pass Variable from function to another python

Time:09-01

This is my code to extract some info based on some condations .

How could i pass Devices variable from def readA( ) to def readB( ) as devices is a avariable will be changed different times .

def readB(x):
    rr = open(('C:/Users/DotNet/Downloads/Backup Configurtion files Test/'  Devices  '-NO trust upstream default77.txt'),"a")
    with open('C:/Users/DotNet/Downloads/Backup Configurtion files Test/'  Devices  '.cfg') as resultFile:
        for line in resultFile:
            if x in line:
                tt = next(resultFile)
                UU = next(resultFile)
                CC = next(resultFile)
                EE = next(resultFile)
                FF = next(resultFile)
                if "interface " in UU:
                    rr.write(x   '\n'   tt)
                    break
                elif "interface " in CC:
                    rr.write(x   '\n'   tt   UU)
                    break
                elif "interface " in EE:
                    rr.write(x   '\n'   tt   UU   CC)
                    break
                elif "interface " in FF:
                    rr.write(x   '\n'   tt   UU   CC   EE)
                    break
                elif "interface " not in FF:
                    rr.write(x   '\n'   tt   UU   CC   EE   FF)
                    break



def readA(Devices):
    with open('C:/Users/DotNet/Downloads/Backup Configurtion files Test/'   Devices   '-NO trust upstream default88.txt') as bondNumberFile:
        for line in bondNumberFile:
            readB(line.rstrip())

readA('10.0.130.30')
readA('10.0.130.20')

CodePudding user response:

Like this? BTW, according to PEP8, arguments and variables names should start with a lowercase letter

def readB(devices, x):
rr = open(('C:/Users/DotNet/Downloads/Backup Configurtion files Test/'   devices   '-NO trust upstream default77.txt'), "a")
with open('C:/Users/DotNet/Downloads/Backup Configurtion files Test/'   devices   '.cfg') as resultFile:
    for line in resultFile:
        if x in line:
            tt = next(resultFile)
            uu = next(resultFile)
            cc = next(resultFile)
            ee = next(resultFile)
            ff = next(resultFile)
            if "interface " in uu:
                rr.write(x   '\n'   tt)
                break
            elif "interface " in cc:
                rr.write(x   '\n'   tt   uu)
                break
            elif "interface " in ee:
                rr.write(x   '\n'   tt   uu   cc)
                break
            elif "interface " in ff:
                rr.write(x   '\n'   tt   uu   cc   ee)
                break
            elif "interface " not in ff:
                rr.write(x   '\n'   tt   uu   cc   ee   ff)
                break


def readA(devices):
    with open('C:/Users/DotNet/Downloads/Backup Configurtion files Test/'   devices   '-NO trust upstream default88.txt') as bondNumberFile:
        for line in bondNumberFile:
            readB(devices, line.rstrip())


readA('10.0.130.30')
readA('10.0.130.20')
  • Related