用变量打开Python文件
我是python的初学者。在下面的代码中,为什么在打开文件和其他位置时使用变量“env”。从教程中,我看到文件打开格式总是像open(filename)一样作为f,但这里使用了一个变量名。请解释一下用变量打开Python文件,python,file,Python,File,我是python的初学者。在下面的代码中,为什么在打开文件和其他位置时使用变量“env”。从教程中,我看到文件打开格式总是像open(filename)一样作为f,但这里使用了一个变量名。请解释一下 def main(): global debug, testing,env, mode newID=0 rv=[] name='' if "debug" in sys.argv: debug = True if &
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)
欢迎普雷马。变量名可以是作者希望的任何名称。通常,程序员应该使用与存储在该变量中的数据相关的描述性变量名 由于这个示例的文档记录得很差,我不得不假设作者使用了“env”来表示这个变量以某种方式连接到某个环境 作者似乎在使用“f_in”变量名来表示该文件是一个输入文件