因业务需要,需要将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
i
in
range
(a
+
1
,b):
#print line[i]
spath.append(line[i].split(
"="
)[
1
].strip(
'\n'
))
#遍历[path]节
for
i
in
range
(b
+
1
,c):
web
=
line[i]
for
i
in
range
(c
+
1
,d):
outpath
=
line[i].strip(
'\n'
)
#.strip('\n') 删除字符串尾部的回车
for
i
in
range
(d
+
1
,
sum
):
outweb
=
line[i].strip(
'\n'
)
outpdfconfig
=
open
(
'%s'
%
(outpath),
'w'
)
#创建配置文件1
outweb_config
=
open
(
'%s'
%
(outweb),
'w'
)
#创建配置文件2
for
i
in
spath:
for
m
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,如需转载请自行联系原作者