使用python生成配置文件

时间:2014-03-19 20:05:27

标签: python

在获取用户输入是否启用了Web访问,手机的分机号,用户名等等之后,我需要使用他们的mac地址作为文件名为手机创建配置文件。我正在尝试创建一个Python脚本允许我在创建后添加到这些配置文件中,以防我需要在现有手机上添加新行。现在,我可以生成配置文件,但如果我尝试编辑它,文件中的文本将被删除。

with open(src_txt_dir + mac + ".txt", "wt") as out:
    if existing_mac:
        shutil.copyfile(src_txt_dir + mac + ".txt", src_txt_dir + mac + "_old.txt")
        start = 1
        for line in open(src_txt_dir + mac + "_old.txt"):
            if 'Line_Enable[' + str(start) + ']"Yes";' in line.replace(' ', ''):
                print start
                start = start + 1
        if os.path.isfile(src_cfg_dir + mac + ".cfg"):
            try:
                os.remove(src_cfg_dir + mac + ".cfg")
            except OSError, e:
                print ("Error: %s - %s" % (e.filename, e.strerror))
        for line in open(src_txt_dir + mac + "_old.txt"):
            print line
            for j in range(start,9):
                line = line.replace('line_enable_' + str(j), line_enabled[j])
                line = line.replace('display_name_' + str(j), name[j])
                line = line.replace('cs_username_' + str(j), uid[j])
                line = line.replace('extension_' + str(j), extension[j])
                line = line.replace('sip_password_' + str(j), sippass[j])
            out.write(line)
    else:
        for line in open(src_txt_dir + "phone_template.txt"):
            line = line.replace('web_enable', web_enable)
            line = line.replace('phone_number', phone_number)
            for i in range(1,9):
                line = line.replace('line_enable_' + str(i), line_enabled[i])
                line = line.replace('display_name_' + str(i), name[i])
                line = line.replace('cs_username_' + str(i), uid[i])
                line = line.replace('extension_' + str(i), extension[i])
                line = line.replace('sip_password_' + str(i), sippass[i])
            out.write(line)

0 个答案:

没有答案