Brennced Brennced - 8 days ago 9
Python Question

Python ConfigParser Invalid Syntax

I'm using the following code to read a config file:


def get_config():
c = configparser.ConfigParser()
c.read("config.cfg")
return c.options("Server")


The file looks like this:


[Server]
port = 9012
workspace = doc/


But I get the following error:


File "config.cfg", line 3
workspace = doc/
^
SyntaxError: invalid syntax


What's causing this and how can I fix it?

Answer

It seems you are trying to run the configuration file as if it were a Python script. Here's one way to reproduce your error:

C:\Users\Luke\StackOverflow>python config.cfg
  File "config.cfg", line 3
    workspace = doc/
                   ^
SyntaxError: invalid syntax
Comments