简体   繁体   中英

python: ConfigParser object and read more then once

Scenario:

I have a config file being maintaining a list of automated tests to be executed. these tests are executed in a long run and in a loop. The config file is designed in a manner so that ConfigParser can read it. As there are two three params i need to pass each test.

Now, this config file is called by a script(s1) and the tests are executed as per the list in config file.

Script(s1) reads the config for the very first time and after each test finishes it's execution.

Requirement to read it twice:

As there may be more test cases that may be added to config file and script need to keep executing. As such the object created by ConfigParser will be once only, but reads can be many a times.

Question is:

The reading of the file more than once in a file. Is it considered to be a good idea in this kind of scenario? Or there can be better way to do it?

Please provide suggestions.

This seems difficult, but really all you need to do is seek your file back to 0 .

from ConfigParser import RawConfigParser

fp = open("config.cfg")
config = RawConfigParser()

config.readfp(fp)

fp.seek(0)

config.readfp(fp)

fp.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM