본문 바로가기
테크/기타

[Python] 파일 read, write 기본

by ahnne 2015. 9. 1.


# Goal.

- python 스크립트를 수행

- 특정 형식의 config파일로부터 읽어온 설정을 기반으로, child process 들을 수행, 관리한다.


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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import re
import sys
import getopt
 
def parent():
        pass
 
def get_port(fh):
        port = {}
        for line in fh:
                line = line.strip()
                strs = re.split("\s+", line)
                if(re.compile("^end$").match(strs[0])):
                        return port
                else:
                        port[strs[0]] = strs[1]
 
 
def init_config():
        filename = "%s/test.file" % BASEDIR
        config = {}
 
def init_config():
        filename = "%s/test.file" % BASEDIR
        config = {}
        config_list = []
        f = open(filename, 'r')
        for line in f:
                line = line.strip()
                strs = re.split("\s+", line)
                if(re.compile("^portclass$").match(strs[0])):
                        print("portclass : %s" % line)
                        port = get_port(f)
                        config_list.append(port)
                #config[kv[0]] = kv[1]
 
                #sys.stdout.write(line) #print witout newline
                #print(line) #print with newline
                #print(line), #print with next space, without newline
        f.close()
        return config_list
 
# main
BASEDIR = None
OTHER = None
 
my_opts, args = getopt.getopt(sys.argv[1:], "b:o:")
command = sys.argv[0]
 
for o, a in my_opts:
        if o == '-b':
                print("-b %s" % a)
                BASEDIR=a
        elif o == '-o':
                print("-o %s" % a)
                OTHER=a
 
if not BASEDIR:
        print("b option required")
        sys.exit()
 
cmd_args = str(sys.argv)
cmd_args_len = len(sys.argv)
 
init_config = init_config()
print("config : %s" % init_config)
 
parent()
 
 
 
 
 
cs