LINUX.ORG.RU

python file read


0

0

from sys import *
from os import *
def ReadConfig(file):
if path.exists(file):
lines=open(file).readlines()
conf_dict={}
for line in lines:
key=line.split('=')[0].strip()
value=line.split('=')[1].strip().replace('\n','')
if not key in conf_dict.keys():
conf_dict[key]=value
else:
if not type(value)==type(list()):
conf_dict[key]=[conf_dict[key]]
conf_dict[key].append(value)
return conf_dict
else: exit('Config file not found')


lines=open(file).readlines()
TypeError: function takes at least 2 arguments (1 given)

В чем собственно дело?

anonymous

Ответ на: комментарий от Davidov

Хм... а резве не ini parser написал топикстартер?

zJes ★★
()

Ксати, конфиг сам по себе может быть питоновской прогой :)

true_admin ★★★★★
()

Выше написавшие правы, ныжно использовать стандартные либы.
А если хочешь велосипед, то лучше пиши так...

def read_file(file_name):
    try:
        file = open(file_name)
    except IOError:
        return None
    result = {}
    for line in file:
        key, value = line.split('=')
        key, value = key.strip(), value.strip()
        try:
            result[key].append(value)
        except KeyError:
            result[key]  = [value]
    return result

options = read_file('config.ini')
if options is None:
    import sys
    print >sys.stderr, 'Config file not found'
    sys.exit(1)

Minmax
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.