我尝试读取一个配置文件并将值赋给变量:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
with open('bot.conf', 'r') as bot_conf:
config_bot = bot_conf.readlines()
bot_conf.close()
with open('tweets.conf', 'r') as tweets_conf:
config_tweets = tweets_conf.readlines()
tweets_conf.close()
def configurebot():
for line in config_bot:
line = line.rstrip().split(':')
if (line[0]=="HOST"):
print "Working If Condition"
print line
server = line[1]
configurebot()
print server
它似乎做得很好,只是它没有给服务器变量赋值
^{pr2}$
server
符号未在您使用的范围内定义。在为了能够打印它,您应该从
configurebot()
返回它。在也可以通过在调用configurebot()之前声明它来使其全局化,如下所示:
^{pr2}$您的
sever
变量是configurebot
函数中的局部变量。在如果要在函数之外使用它,必须使它^{} 。在
相关问题 更多 >
编程相关推荐