configparser

ConfigParser vs. import config

两盒软妹~` 提交于 2019-12-03 14:33:42
问题 ConfigParser is the much debated vanilla configuration parser for Python. However you can simply import config where config.py has python code which sets configuration parameters. What are the pros\cons of these two approaches of configuration? When should I choose each? 回答1: The biggest issue I see with import config is that you don't know what will happen when you import it. Yes, you will get a set of symbols that are naturally referenced using a . style interface. But the code in the

Update INI file without removing comments

你。 提交于 2019-12-03 11:17:39
问题 Consider the following INI file: [TestSettings] # First comment goes here environment = test [Browser] # Second comment goes here browser = chrome chromedriver = default ... I'm using Python 2.7 to update the ini file: config = ConfigParser.ConfigParser() config.read(path_to_ini) config.set('TestSettings','environment',r'some_other_value') with open(path_to_ini, 'wb') as configfile: config.write(configfile) How can I update the INI file without removing the comments. The INI file is updated

ConfigObj/ConfigParser vs. using YAML for Python settings file

ⅰ亾dé卋堺 提交于 2019-12-03 09:45:40
Which is better for creating a settings file for Python programs, the built-in module (ConfigParser) or the independent project (ConfigObj), or using the YAML data serialization format? I have heard that ConfigObj is easier to use than ConfigParser, even though it is not a built-in library. I have also read that PyYAML is easy to use, though YAML takes a bit of time to use. Ease of implementation aside, which is the best option for creating a settings/configuration file? Using ConfigObj is at least very straightforward and ini files in general are much simpler (and more widely used) than YAML.

Python ConfigParser interpolation from foreign section

折月煮酒 提交于 2019-12-03 06:01:12
With Python ConfigParser, is it possible to use interpolation across foreign sections? My mind seems to tell me I've seen that it's possible somewhere, but I can't find it when searching. This example doesn't work, but it's to give an idea of what I'm trying to do. [section1] root = /usr [section2] root = /usr/local [section3] dir1 = $(section1:root)/bin dir2 = $(section2:root)/bin Note that I'm using Python 2.4. chown In python 3.2 and up this is perfectly valid: [Common] home_dir: /Users library_dir: /Library system_dir: /System macports_dir: /opt/local [Frameworks] Python: 3.2 path: $

Python 2.4.3: ConfigParser.NoSectionError: No section: 'formatters'

◇◆丶佛笑我妖孽 提交于 2019-12-03 05:36:16
Trying to use a logging configuration file to implement TimedRotatinigFileHandler . Just won't take the config file for some reason. Any suggestions appreciated. x.py: import logging import logging.config import logging.handlers logging.config.fileConfig("x.ini") MyLog = logging.getLogger('x') MyLog.debug('Starting') x.ini: [loggers] keys=root [logger_root] level=NOTSET handlers=trfhand [handlers] keys=trfhand [handler_trfhand] class=handlers.TimedRotatingFileHandler when=M interval=1 backupCount=11 formatter=generic level=DEBUG args=('/var/log/x.log',) [formatters] keys=generic [formatter

ConfigParser vs. import config

回眸只為那壹抹淺笑 提交于 2019-12-03 04:21:29
ConfigParser is the much debated vanilla configuration parser for Python. However you can simply import config where config.py has python code which sets configuration parameters. What are the pros\cons of these two approaches of configuration? When should I choose each? The biggest issue I see with import config is that you don't know what will happen when you import it. Yes, you will get a set of symbols that are naturally referenced using a . style interface. But the code in the configuration file can also do who-knows-what. Now, if you completely trust your users, then allowing them to do

Update INI file without removing comments

风流意气都作罢 提交于 2019-12-03 02:45:31
Consider the following INI file: [TestSettings] # First comment goes here environment = test [Browser] # Second comment goes here browser = chrome chromedriver = default ... I'm using Python 2.7 to update the ini file: config = ConfigParser.ConfigParser() config.read(path_to_ini) config.set('TestSettings','environment',r'some_other_value') with open(path_to_ini, 'wb') as configfile: config.write(configfile) How can I update the INI file without removing the comments. The INI file is updated but the comments are removed. [TestSettings] environment = some_other_value [Browser] browser = chrome

What's better, ConfigObj or ConfigParser?

梦想的初衷 提交于 2019-12-03 02:30:49
Which is better for creating a settings file for Python programs, the built-in module ( ConfigParser ), or the independent project ( ConfigObj )? rdchambers I recently switched from configparser to configobj, and I'm thrilled to have done so. For me, the big difference is configobj's validator . It lets me very easily/succinctly (1) define the type and acceptable values for each entry, and (2) set defaults. Those two features save me a lot of code and prevent a lot from going wrong. Plus, there's really no extra overhead to using configobj in terms of the complexity of my code, and the library

How to remove spaces while writing in INI file- Python

放肆的年华 提交于 2019-12-01 19:25:37
I am using a file and i have one section named DIR in which it contain the paths. EX: [DIR] DirTo=D:\Ashish\Jab Tak hai Jaan DirBackup = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Backup ErrorDir = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Error CombinerDir = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Combiner DirFrom=D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\In PidFileDIR = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Pid LogDir = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Log TempDir = D:\Parser\ERICSSON_CSCORE_STANDARD_VMS\Temp Now I want to replace the paths which I have done it but when I replaced its giving me

Stop ConfigParser adding spaces to delims after upgrade from python 2.7.3 to 2.7.9

扶醉桌前 提交于 2019-12-01 14:06:57
After being forced to use a later version of python, ConfigParser now insists on adding spaces to each side of any delims when modifying a configuration file. e.g. setting=90 becomes: setting = 90 This was not the behavior in the earlier version, and I cannot find a way of controlling this behavior, can anyone help? My test code looks like this: import ConfigParser import os config = ConfigParser.ConfigParser() cfgfile = '/home/osmc/bin/test/config.txt' os.system('sudo echo "[section]" > ' + cfgfile) os.system('sudo echo "setting=0" >> ' + cfgfile) config.read(cfgfile) config.set('section',