0

I have very simple problem I want to print everything that's inside session.ini

But it doesn't let me the output is: >> {}

Does somebody have a trick how to print everything inside the session.ini

inside: session.ini

[Session 1]
Data = "session_1.db"

[Session 2]
Data = "session_1.db"

The code:

session = ConfigObj("sessions.ini")

def Session():
    for item in session:
      print(item)
Frederik
  • 409
  • 1
  • 5
  • 19

1 Answers1

1

The following works for me with Python 2.7.5 and your .ini file, so I can't reproduce the problem. Have I missed something?

Note: You're opening a file called sessions.ini but your question shows it under a heading that says session.ini -- without the 's'. That might be the cause...

from configobj import ConfigObj

session = ConfigObj("sessions.ini", raise_errors=True)

def Session():
    for item in session:
        print(item)

Session()

Output:

Session 1
Session 2
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thank you that was the problem. This only printed the headlines Session 1, Session 2 What if I want to print the lines under like Data = "session_1.db" – Frederik Sep 20 '13 at 18:23
  • `ConfigObj` is a subclass of `Section`, which is a subclass of `dict`, so you could use `for n, s in session.iteritems():` where each `n` is a `Section`'s name and each `s` is a `Section` object -- which, like a nested `dict`, would need a nested `for` to iterate over its contents (name, value pairs). – martineau Sep 20 '13 at 19:25
  • Understood. What if I wanted to print what `Data` is assigned to in this case: `Session_1.db` `Session_2.db` And not everything `{'Data': 'session_1.db'}` Sorry for these questions after each other should posted this in one comment. – Frederik Sep 20 '13 at 20:14
  • `print session['Session 1']['Data']` --> `session_1.db`. A `ConfigObj` behaves a lot like a dictionary of dictionaries. The [online documention](http://www.voidspace.org.uk/python/configobj.html) is pretty good, check it out. – martineau Sep 20 '13 at 20:56