Python的INI文件操作

Ini 文件操作中有一个优势是它能够直接被Python分区,并且update. 否则如果是普通的txt文件,我们会需要read line by line. 但是在ini 的操作中我们只需要阅读分区,然后再update分区就好。

例如下面的这个ini文件:

[Mandatory]
file = 	C:\Users\xhan\Desktop\a.file

[Scenarios]
enabled = True
enable_list = scenario

这其中就包含了两个分区 [Mandatory] 与 [Scenario]. 我们可以直接通过Python内置的ConfigParser 来实现 ini 的update.

ConfigParser的操作:

-read(filename) 直接读取文件内容
-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到该section的所有option
-items(section) 得到该section的所有键值对

首先实例化一个configparser对象 config:

config = ConfigParser.RawConfigParser()

然后再将文件读入:

config.read(inifile)

然后其中的Sections就会返回 [Mandatory, Scenario], return type: ‘list’

Options (‘Mandatory’) 返回的是 “file”, return type: ‘list’

Items (‘Mandatory’) 返回得是 “(file, ‘’)”, 其实就是option’s key + option’s value

但是我们一般来说通过阅读 ini file 已经明白了这些,因此我们仅仅需要使用如下方式去读取/修改 ini 文件:

-get(section,option) 得到section中option的值,返回为string类型
-getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
-set (section, option, value) : update the ‘section’ ‘option’ 的 key 对应的键值成为新的给的value. 注意的是 set之后必须通过 write写入,否则不会修改
-add_section(section) 添加一个新的section
-remove_section(section) 删除某个 section
-remove_option(section, option) 删除某个 section 下的 option

例如,下面这个就是update了我的 LAF analyzer中的 log的路径:

config.set ('Mandatory', 'file', '\t' + file)

最后打开文件后将我们做的update写入到file当中:

config.write(open(inifile, 'w'))

在我的例子中,我需要的是读取原来的LAF config file,之后不断的 update 它的文件路径。然后让python去run 它。实例如下


def launch_laf_pp(laf_config_filepath):
    try:
        cmd = 'python ' + lafpath + '\\LogPacketTranslator.py -c ' + laf_config_filepath
        print cmd
        os.system(cmd)
    except:
        pass

def LAF_handle (file, inifile):
    # change the file location in config file and call LAF to run it:
    config = ConfigParser.RawConfigParser()
    config.read(inifile)
    config.set ('Mandatory', 'file', '\t' + file)
    config.write(open(inifile, 'w'))
    launch_laf_pp(inifile)

def checkandlauchlteftlafpp(tx_destpath, rx_destpath):
    tx_files = [tx_destpath + '\\' + f for f in os.listdir(tx_destpath) if f.endswith('.isf')]
    rx_files = [rx_destpath + '\\' + f for f in os.listdir(rx_destpath) if f.endswith('.isf')]
    return tx_files, rx_files


def main():
    tx_files, rx_files = checkandlauchlteftlafpp(tx_file_path, rx_file_path)
    print len(tx_files), len(rx_files)
    if len(tx_files) != len(rx_files):
        print 'the number of Tx files and number of Rx files is not the same, skip the process'
        time.sleep(30)
    else:
        print "Tx files and Rx file found, the number matches, goes to LAF handler"
    # Call LAF to process the file generate the CSV file
    for i in xrange(len(tx_files)):
        LAF_handle(rx_files[i], 'Rx.ini')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值