티스토리 뷰

설정값이나, 이번값(default value)등을 자동으로 setting 해주기 위해서 아래와 같은 형식의 .ini 파일을 만들었다. 파이썬으로 이 .ini 의 값을 불러오는 코드와 다시 .ini 에 wirte 하는 code 이다.

fileRoot=c:\files
fileCount=5
...



 #/usr/bin/env python
# -*- coding: mbcs -*-

"""
This simple example is used for the line-by-line tutorial
that comes with pygame. It is based on a 'popular' web banner.
Note there are comments here, but for the full explanation,
follow along in the tutorial.
"""

import os, sys
import string
import re # regular expression
import codecs


# rxin = raw_input('enter a regex to search for:\n')


debug = False




def main(argv=None) :

    if argv is None:
        argv = sys.argv
        argc = len(argv)

    if argc < 2 :
        print "Usage: run.py [dz_file_path]"
        sys.exit()

    filename = argv[1]
    # newSwVersion = argv[2]
    # newCompileDate = argv[3]
    # newHiddenVer = argv[4]
    print read(filename)


## Read the values from the file
# @param filename initial setting file name, which has the value for the default value
# @param seperator this seperates the variable names and the values, for instance, a=3
#
# @return configDic dictionary type is returned
def read(filename, seperator='=') :
    try:
        rf = codecs.open(filename, "rb")
    except IOError:
        print filename + " does not exist"
    else:
        #///< when the file exists
        configDic = {}
        for rawLine in rf:
            l = rawLine.strip().split(seperator) # '=' is seperator
            if l is '' :
                continue
            configDic[l[0]] = l[1]

        rf.close()
        return configDic


## Write back the values to the file
# @param filename file name at which values are written
# @param lastConfig dictionary type which has the values
def write(filename, lastConfig={}) :
    try:
        wf = codecs.open(filename, "wb", "mbcs")
    except IOError:
        print filename + " does not exist"
    else:
        #///< when the file exists
        for key in lastConfig:
        wf.write(key + "=" + lastConfig[key] + '\n')

        wf.close()
        return


if __name__ == '__main__' :
    main()


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
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
글 보관함