简体   繁体   English

将变量从 function 传递到另一个 python

[英]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.我如何将 Devices 变量从 def readA() 传递到 def readB(),因为 devices 是一个变量,将在不同时间更改。

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

Like this?像这样? BTW, according to PEP8 , arguments and variables names should start with a lowercase letter顺便说一句,根据PEP8 , arguments 和变量名称应以小写字母开头

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM