Properties file in python (similar to Java Properties)

后端 未结 25 2773
故里飘歌
故里飘歌 2020-11-29 17:41

Given the following format (.properties or .ini):

propertyName1=propertyValue1
propertyName2=propertyValue2
...
propertyNam         


        
25条回答
  •  情深已故
    2020-11-29 18:20

    I did this using ConfigParser as follows. The code assumes that there is a file called config.prop in the same directory where BaseTest is placed:

    config.prop

    [CredentialSection]
    app.name=MyAppName
    

    BaseTest.py:

    import unittest
    import ConfigParser
    
    class BaseTest(unittest.TestCase):
        def setUp(self):
            __SECTION = 'CredentialSection'
            config = ConfigParser.ConfigParser()
            config.readfp(open('config.prop'))
            self.__app_name = config.get(__SECTION, 'app.name')
    
        def test1(self):
            print self.__app_name % This should print: MyAppName
    

提交回复
热议问题