我有一个脚本,它调用我正在整理的linux来宾列表。以下是代码:
#!/usr/bin/python
guests = ['guest1','guest2','guest3','guest*']
def serverCheck(guestList)
for g in guestList:
server = AdminControl.completeObjectName('cell=tstenvironment,node=guest1,name=uatenvironment,type=Server,*')
try:
status = AdminControl.getAttribute(server, 'state')
print g + status
except:
print "Error %s is down." % g
serverCheck(guests)问题就在这一行上:
server = AdminControl.completeObjectName('cell=Afcutst,node=%s,name=afcuuat1,type=Server,*') % g如何使用我的列表填充节点变量,同时仍然能够将括号内的信息传递给AdminControl函数?
发布于 2016-03-17 20:45:32
参数字符串本身是%操作符的参数,而不是函数调用的返回值。
server = AdminControl.completeObjectName(
'cell=Afcutst,node=%s,name=afcuuat1,type=Server,*' % (g,)
)通过窥视水晶球,Python 3.6将允许您编写
server = AdminControl.completeObjectName(
f'cell=Afcutst,node={g},name=afcuuat1,type=Server,*'
)将变量直接嵌入到特殊格式字符串文本中。
发布于 2016-03-17 21:03:13
你能这样试试吗?
AdminControl.completeObjectName('cell=tstenvironment,node=%s,name=uatenvironment,type=Server,*'%g)发布于 2016-03-17 21:11:14
为了获得更多的可读性,我建议这样做,并使用相同的方式从变量格式化字符串(在这里,我选择了str.format)
guests = ['guest1','guest2','guest3','guest*']
def serverCheck(guestList)
name_tpl = 'cell=tstenvironment,node={},name=uatenvironment,type=Server,*'
for g in guestList:
obj_name = name_tpl.format(g)
server = AdminControl.completeObjectName(obj_name)
try:
status = AdminControl.getAttribute(server, 'state')
print '{}: {}'.format(g, status)
except:
print 'Error {} is down'.format(g)
serverCheck(guests)https://stackoverflow.com/questions/36071302
复制相似问题