Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass Variable from function to another python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading