简体   繁体   English

python:ConfigParser对象,然后再阅读一次

[英]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. 配置文件的设计方式使ConfigParser可以读取它。 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)调用,并且按照配置文件中的列表执行测试。

Script(s1) reads the config for the very first time and after each test finishes it's execution. Script(s1)第一次读取配置,并且在每次测试完成后都会执行。

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. 这样,由ConfigParser创建的对象将只有一次,但是读取可以多次。

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 . 这似乎很难,但实际上所有你需要做的是seek你的文件恢复到0

from ConfigParser import RawConfigParser

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

config.readfp(fp)

fp.seek(0)

config.readfp(fp)

fp.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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