Python read in config file

How to read a config file using python

Inventing your own formats, even if they look simple like that, is not the best idea generally. Better stick to a builtin one, there are plenty of choices: .ini, json, yaml, xml.

7 Answers 7

In order to use my example, your file «abc.txt» needs to look like this.

[your-config] path1 = "D:\test1\first" path2 = "D:\test2\second" path3 = "D:\test2\third" 

Then in your code you can use the config parser.

import ConfigParser configParser = ConfigParser.RawConfigParser() configFilePath = r'c:\abc.txt' configParser.read(configFilePath) 

As human.js noted in his comment, in Python 3, ConfigParser has been renamed configparser . See Python 3 ImportError: No module named ‘ConfigParser’ for more details.

I’ve edited the post, my suggestion is for using config files it much easier and very useful, that way you can learn a new thing that can really help you. the error MissingSectionHeaderError was beacuse you need the section [file]

@KobiK I’m able to get the path of the currently executed script file by os.path.abspath(os.path.dirname(sys.argv[0])). Then I could do an os.path.join with my «abc.txt». That solved the issue. Thanks a lot

in python 3, ConfigParser is renamed to configparser. Check here for details stackoverflow.com/questions/14087598/…

You may want to remove quotes from your config file variables content since using configParser.get(‘your-config’, ‘path1’) will add another stack of (single) quotes (tested on python3 configparser)

You need a section in your file:

[My Section] path1 = D:\test1\first path2 = D:\test2\second path3 = D:\test2\third 
import ConfigParser config = ConfigParser.ConfigParser() config.readfp(open(r'abc.txt')) path1 = config.get('My Section', 'path1') path2 = config.get('My Section', 'path2') path3 = config.get('My Section', 'path3') 

If you need to read all values from a section in properties file in a simple manner:

Your config.cfg file layout :

[SECTION_NAME] key1 = value1 key2 = value2 
 import configparser config = configparser.RawConfigParser() config.read('path_to_config.cfg file') details_dict = dict(config.items('SECTION_NAME')) 

This will give you a dictionary where keys are same as in config file and their corresponding values.

Now to get key1’s value : details_dict[‘key1’]

Putting it all in a method which reads sections from config file only once(the first time the method is called during a program run).

def get_config_dict(): if not hasattr(get_config_dict, 'config_dict'): get_config_dict.config_dict = dict(config.items('SECTION_NAME')) return get_config_dict.config_dict 

Now call the above function and get the required key’s value :

config_details = get_config_dict() key_1_value = config_details['key1'] 

Generic Multi Section approach:

[SECTION_NAME_1] key1 = value1 key2 = value2 [SECTION_NAME_2] key1 = value1 key2 = value2 

Extending the approach mentioned above, reading section by section automatically and then accessing by section name followed by key name.

def get_config_section(): if not hasattr(get_config_section, 'section_dict'): get_config_section.section_dict = collections.defaultdict() for section in config.sections(): get_config_section.section_dict[section] = dict(config.items(section)) return get_config_section.section_dict 
config_dict = get_config_section() port = config_dict['DB']['port'] 

(here ‘DB’ is a section name in config file and ‘port’ is a key under section ‘DB’.)

A convenient solution in your case would be to include the configs in a yaml file named **your_config_name.yml** which would look like this:

path1: "D:\test1\first" path2: "D:\test2\second" path3: "D:\test2\third" 

In your python code you can then load the config params into a dictionary by doing this:

import yaml with open('your_config_name.yml') as stream: config = yaml.safe_load(stream) 

You then access e.g. path1 like this from your dictionary config:

To import yaml you first have to install the package as such: pip install pyyaml into your chosen virtual environment.

This looks like valid Python code, so if the file is on your project’s classpath (and not in some other directory or in arbitrary places) one way would be just to rename the file to «abc.py» and import it as a module, using import abc . You can even update the values using the reload function later. Then access the values as abc.path1 etc.

Of course, this can be dangerous in case the file contains other code that will be executed. I would not use it in any real, professional project, but for a small script or in interactive mode this seems to be the simplest solution.

Just put the abc.py into the same directory as your script, or the directory where you open the interactive shell, and do import abc or from abc import * .

Thanks, but I don’t have a project build and all. I’m just running the Python script from commandline only. Is it possible to do something like this in this case ? I mean is there any other way to make these together as a package or something like that ?

@user2882117 Sure, just put the abc.py in the same directory as your script, or the directory you start an interactive shell in, and do import abc . Particularly if it’s just for a small script, I think this is the simplest solution, but I would not use it in a «real» project.

Источник

How to read config from string or list?

Is it possible to read the configuration for ConfigParser from a string or list?
Without any kind of temporary file on a filesystem OR
Is there any similar solution for this?

3 Answers 3

You could use a buffer which behaves like a file: Python 3 solution

import configparser import io s_config = """ [example] is_real: False """ buf = io.StringIO(s_config) config = configparser.ConfigParser() config.read_file(buf) print(config.getboolean('example', 'is_real')) 

In Python 2.7, this implementation was correct:

import ConfigParser import StringIO s_config = """ [example] is_real: False """ buf = StringIO.StringIO(s_config) config = ConfigParser.ConfigParser() config.readfp(buf) print config.getboolean('example', 'is_real') 

cStringIO objects are not buffer s. Strings are buffers, but buffers cannot be used where file -like objects are required; cStringIO wraps a buffer in order to make it behave like a file . Besides, your example does not demonstrate how a cStringIO behaves like a file; getvalue is a method specific to cStringIO instances, but files don’t have it.

The question was tagged as python-2.7 but just for the sake of completeness: Since 3.2 you can use the ConfigParser function read_string() so you don’t need the StringIO method anymore.

import configparser s_config = """ [example] is_real: False """ config = configparser.ConfigParser() config.read_string(s_config) print(config.getboolean('example', 'is_real')) 

To be fair to RHEL7, it’s perfectly possibly to install Python 3.x. developers.redhat.com/blog/2018/08/13/install-python3-rhel

Python has read_string and read_dict since version 3.2. It does not support reading from lists.

The example shows reading from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section.

#!/usr/bin/env python3 import configparser cfg_data = < 'mysql': > config = configparser.ConfigParser() config.read_dict(cfg_data) host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print(f'Host: ') print(f'User: ') print(f'Password: ') print(f'Database: ') 

Источник

How to read data from .conf file using python?

How do I read the data from the config file using python? Is there any module in python? Thanks in advance!

It just read the data jia Jimmy ..For example if i need to connect mysql then it read its host,username and password,database name from the .conf file

2 Answers 2

If the standard .ini format can be followed in your config file,

[read] host => "localhost" port => "8080" 

Then use configparser library.

from configparser import ConfigParser data_file = 'sample.conf' config = ConfigParser() config.read(data_file) print(config.sections()) print(config['read']['host']) 

Thanks for your answer But i need to read .conf for further process Sleeba paul .Do you have any idea regarding that?

Sorry for the late reply Sleepa.. section read is of list datatype .what about the type of variables host and port?? Do they also belongs to list ??Can you please clarify me

You write a standard .ini file with the format I mentioned in the answer. [read] is not list dtype but the usually an env variable. Say, database variables like database URL comes under [database] and so on.

honestly it depends on what you mean by «read the file» if you are talking about reading it like any other text file just use something like

with open("example.conf", 'r') as f: print(f.read()) 

the code above reads the file as a text file however if you want to read it as a .conf file do something like this

from configparser import ConfigParser data_file = 'sample.conf' config = ConfigParser() config.read(data_file) print(config.sections()) print(config['read']['host']) 

Источник

Читайте также:  Java get method with annotation
Оцените статью