Python 读取配置文件 生成配置文件 试例

简介:

因业务需要,需要将inputconfig.ini 如下

1
2
3
4
5
6
7
8
9
10
[path]
1 =f:\aaa\1989works
2 =f:\bbb\1990works
3 =f:\ccc\1991works
[web]
zz.xxx.yyy.com
[outpath]
E:\createconfig\papers.ini
[outweb]
E:\createconfig\web_papers.ini

作为输入 然后生成2个配置文件,papers.ini,web_papers.ini

其中papers.ini内容为

1
2
3
4
5
6
7
eee=f:\aaa\1989works
fff=f:\pdf\1989works
mmmm=f:\pdf\1989works
ccc=f:\bbb\1990works
...
...
...

既,配置文件等号左边为path节的等号右边的目录遍历一级目录的内容,等号右边为path节的等号右边的内容。

web_papers.ini的内容为:

1
2
3
4
eee=zz.xxx.yyy.com
fff=zz.xxx.yyy.com
mmmm=zz.xxx.yyy.com
ccc=zz.xxx.yyy.com

既,等号左边的内容为path节的等号右边的目录遍历一级目录的内容,等号右边的内容为[web]节的内容。

希望为可执行文件,采用

xxx.exe -i inputconfig.ini 执行既可实现生成输出的2个配置。

先用python写脚本,然后将python脚本编译为单个可执行文件。

【python脚本内容】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#encoding=utf-8
#author: skybug
#date: 2014-01-13
#function: 生成xxx站点配置文件
import  os,sys,getopt
def  createpdfconfig(infile):   #定义函数
     while  True  :
         line = infile.readlines()  #输入文件一次      读入到数组
         if  len (line) = = 0 : break
         else :
             spath = []
             sum = len (line)   #计算数组长度
             a =  line.index( '[path]\n' #定位[path]节在数组的位置
             b =  line.index( '[web]\n' #定位[web]节在数组的位置
             c =  line.index( '[outpath]\n' #定位[outpath]节在数组的位置
             d =  line.index( '[outweb]\n' #定位[outweb]节在数组的位置
             for  in  range (a + 1 ,b):
             #print line[i]
                 spath.append(line[i].split( "=" )[ 1 ].strip( '\n' ))  #遍历[path]节
             for  in  range (b + 1 ,c):
                 web =  line[i]
             for  in  range (c + 1 ,d):
                 outpath =  line[i].strip( '\n' #.strip('\n') 删除字符串尾部的回车
             for  in  range (d + 1 , sum ):
                 outweb = line[i].strip( '\n' )
                 outpdfconfig = open ( '%s' % (outpath), 'w' )   #创建配置文件1
                 outweb_config = open ( '%s' % (outweb), 'w' )   #创建配置文件2
             for  in  spath:
                 for  in  os.listdir(i):
                     outpdfconfig.write( '%s=%s' % (m,i))   #写配置文件1内容
                     outpdfconfig.write( '\n' )
                     outpdfconfig.flush()
                     outweb_config.write( '%s=%s' % (m,web))  #写配置文件2内容
                     outweb_config.flush()
                                                                  
     outpdfconfig.close()  #关闭文件
     outweb_config.close()
     infile.close()
     return  ( 0 , 'OK' )
opts, args  =  getopt.getopt(sys.argv[ 1 :],  "hi:" #解析输入参数
for  op,value  in  opts:
     if  op  = =  "-i" :
         inputconfig = value
         infile = open ( '%s' % (inputconfig), 'r' )
         createpdfconfig(infile)
     else :
         print  'pleas -i configfile'
         sys.exit()

【将xxx.py编译为单个exe文件】

采用pyinstall2进行编译。

具体编译方法转载如下:

http://www.cnblogs.com/balian/archive/2012/11/21/2780503.html

python pyinstaller.py -F E:\temp\papers\createconfig.py

既可生成可执行exe文件


      本文转自天山三害 51CTO博客,原文链接:http://blog.51cto.com/skybug/1351361,如需转载请自行联系原作者





相关文章
|
7月前
|
Python
百度搜索:蓝易云【Python 使用ConfigParser操作ini配置文件教程。】
请注意,实际的使用可能涉及更复杂的配置文件结构和操作。你可以参考 `ConfigParser`的官方文档以获取更多详细信息和示例。
236 0
|
1月前
|
存储 XML JSON
Python如何读写配置文件?
Python如何读写配置文件?
22 0
|
3月前
|
存储 BI 网络安全
正在等待继续编辑 - Python - 基础知识专题 - 配置文件与日志管理
正在等待继续编辑 - Python - 基础知识专题 - 配置文件与日志管理
21 0
|
8月前
|
JSON 关系型数据库 MySQL
Python--toml配置文件
Python--toml配置文件
120 0
|
7月前
|
关系型数据库 MySQL 测试技术
3分钟学会Python 常用配置文件处理
### 1\. 什么是配置文件 配置文件是为程序配置参数和初始设置的文件。一般为文本文件,以`ini`,`conf`,`cnf`,`cfg`,`yaml`等作为后缀名。
|
8月前
|
数据格式
Python--配置文件优化
Python--配置文件优化
29 0
|
8月前
|
关系型数据库 MySQL 数据处理
Python--Yaml配置文件
Python--Yaml配置文件
67 0
|
8月前
|
JSON 关系型数据库 MySQL
Python--json配置文件
Python--json配置文件
83 0
|
8月前
|
关系型数据库 MySQL 数据库
Python--py配置文件
Python--py配置文件
46 0
|
8月前
|
关系型数据库 MySQL 数据库
Python--ini配置文件
Python--ini配置文件
68 0

热门文章

最新文章