Python read config file key




















Storing the secrets passwords in a separate file Ask Question. Asked 7 years, 4 months ago. Active 6 months ago. Viewed 63k times. Incerteza Incerteza What about environment variables? JoelHermanns What about environment variables? Possible duplicate of I need to securely store a username and password in Python, what are my options?

Probably the best method: stackoverflow. Add a comment. Active Oldest Votes. Example would look like this config. Be aware that arbitrary code in config. This is a concern especially if main. Jonathan In that case, all the code in config is still run.

It is very readable and gives you a couple different ways to represent the same data. For your problem, you could create a YAML file that looks like this:. At this point errors is a Python dictionary with the expected format. YAML is capable of representing more than dictionaries: if you prefer a list of pairs, use this format:. Either one sounds like it will fit your current needs, ConfigParser will be easier to start with while YAML gives you more flexibilty in the future, if your needs expand.

I've heard that ConfigObj is easier to work with than ConfigParser. It is used by a lot of big projects, IPython, Trac, Turbogears, etc From their introduction :. ConfigObj is a simple but powerful config file reader and writer: an ini file round tripper. Its main feature is that it is very easy to use, with a straightforward programmer's interface and a simple syntax for config files.

It has lots of other features though :. I think you want the ConfigParser module in the standard library. It reads and writes INI style files. The examples and documentation in the standard documentation I've linked to are very comprehensive.

If you are the only one that has access to the configuration file, you can use a simple, low-level solution. Keep the "dictionary" in a text file as a list of tuples regexp, message exactly as if it was a python expression: [ "file.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Python: Read configuration file with multiple lines per key Ask Question.

Asked 11 years, 11 months ago. Active 3 years, 7 months ago. Viewed 20k times. EDIT: ConfigParser is great, but it forces me to add a space or tab at the beginning of every line in a multiline entry. Thanks in advance, Adam. Adam Matan. Adam Matan Adam Matan k gold badges silver badges bronze badges. Can you elaborate?

Didn't understand your comment. Add a comment. Active Oldest Votes. ConfigParser config. Add a comment. Active Oldest Votes. ConfigParser config.

INI', 'w' as configfile: save config. Rik Poggi Rik Poggi Gives configparser. MissingSectionHeaderError when using provided example files without the proper section headers.

Here's a complete read, update and write example. Input file, test. Agostino Agostino 2, 7 7 gold badges 39 39 silver badges 64 64 bronze badges. On my Python 3. Excellent example, thanks! Alex Alex 2 2 bronze badges. Community Bot 1 1 1 silver badge.

Robert Siemer Robert Siemer If after the.



0コメント

  • 1000 / 1000