Reputation: 5
I try to read a config file and assign the values to variables:
#!/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
it seems to do all fine except it doesn't assign any value to the server variable
ck@hoygrail ~/GIT/pptweets2irc $ ./testbot.py
Working If Condition
['HOST', 'irc.piratpartiet.se']
Traceback (most recent call last):
File "./testbot.py", line 23, in <module>
print server
NameError: name 'server' is not defined
Upvotes: 0
Views: 800
Reputation: 6448
server
symbol is not define in the scope you use it.
To be able to print it, you should return it from configurebot()
.
#!/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
return line[1]
print configurebot()
You can also make it global by declaring it before the call to configurebot() as followed:
server = None
configurebot()
print server
Upvotes: 1