0

I am a beginner in python . why the variable "env" being used while opening the file and other places in the following code . From the tutorials i have seen file opening formats were always like with open(filename) as f , but here a variable name is being used. Please explain .

def main():
    global debug, testing,env, mode
    newID=0
    rv=[]
    name=''

    if "debug" in sys.argv:
        debug = True
    if "testing" in sys.argv:
        testing = True
    if debug:
        print(sys.argv)

    if "copyAccountProfile" in sys.argv:
        mode = 'copyAccountProfile' 

    envProfile = sys.argv[1]
 
    if debug:
        print(envProfile)

    with open(envProfile) as f_in: 
        lineList = list(line for line in (l.strip() for l in f_in) if line)
#    ff = open(envProfile)
#    lineList = [line for line in ff.readlines() if line.strip()]
    for line in lineList:
        if line.startswith('#'):
            continue
        [nm, v]=line.strip().split('=')
        env[nm.strip()]=v.strip()
        if debug:
            print(env)

    with open(env['accountProfile']) as f_in: 
        accountList = list(line for line in (l.strip() for l in f_in) if line)
    if mode == 'copyData':  
        for acc in accountList:
            [name,Id] = acc.strip().split(',')
            name = name.strip()
            #copyData(name, env['src_ror'], workingDir, env['dst_ror'], workingDir, 'bk_files', True)         
            copyData(name, env['src_ror'], workingDir, env['dst_ror'], workingDir, 'bk_files')         
            copyData(name, env['src_ror'], workingDir, env['dst_bpm'], backupDir, 'bk_files')         
            copyData(name, env['src_bpm'], workingDir, env['dst_bpm'], workingDir, 'ft_files')         
            localCopyData(name, env['dst_bpm'], workingDir, backupDir) 
    else:
    #copy updated accountProfile from dst_ror to dst_bpm
        fname = env['accountProfile']
        # src = ip:port, dst = ip:port}
        [sIp, sPort]=env['dst_ror'].split(':')
        [dIp, dPort]=env['dst_bpm'].split(':')
        copyFile(fname, sIp, sPort, toolDir, dPort, dIp, toolDir) 
    

1 Answer 1

1

Welcome Prema. Variable names can be anything the author wishes them to be. Generally, a programmer should use descriptive variable names that relate to the data being stored in that variable.

Since this example is poorly documented, I'll have to assume that the author used "env" to show that this variable is somehow connected to an environment of some sort.

This author seems to be using the "f_in" variable name to indicate the file is an input file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.