van neilsen
van neilsen

Reputation: 547

Read a line from a file in python

I have one file named mcelog.conf and I am reading this file in my code. Contents of the file are

no-syslog = yes   # (or no to disable)
logfile = /tmp/logfile

Program will read the mcelog.conf file and will check for the no-syslog tag, if no-syslog = yes then program has to check for the tag logfile and will read the logfile tag. Can anyone let me know how I can get the value /tmp/logfile

with open('/etc/mcelog/mcelog.conf', 'r+') as fp:
    for line in fp:
        if re.search("no-syslog =", line) and re.search("= no", line):
            memoryErrors = readLogFile("/var/log/messages")
            mcelogPathFound = true
            break
        elif re.search("no-syslog =", line) and re.search("= yes", line):
            continue
        elif re.search("logfile =", line):  
            memoryErrors = readLogFile(line)   # Here I want to pass the value "/tmp/logfile" but currently "logfile = /tmp/logfile" is getting passed
            mcelogPathFound = true
            break
fp.close()

Upvotes: 0

Views: 2033

Answers (3)

Gaurav Dhama
Gaurav Dhama

Reputation: 1336

Change the code to:

with open('/etc/mcelog/mcelog.conf', 'r+') as fp:
    for line in fp:
        if re.search("no-syslog =", line) and re.search("= no", line):
            memoryErrors = readLogFile("/var/log/messages")
            mcelogPathFound = true
            break
        elif re.search("no-syslog =", line) and re.search("= yes", line):
            continue
        elif re.search("logfile =", line):  
            emoryErrors = readLogFile(line.split("=")[1].strip())   # Here I want to pass the value "/tmp/logfile" but currently "logfile = /tmp/logfile" is getting passed
            mcelogPathFound = true
            break
 fp.close()

This is because you want to read only a part of the line rather the whole thing so I have just split it up by the "=" sign and then stripped it to remove any blanks

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191681

I liked the suggestion of the configparser module, so here is an example of that (Python 3)

For the given input, it will output reading /var/log/messages

import configparser, itertools
config = configparser.ConfigParser()
filename = "/tmp/mcelog.conf"

def readLogFile(filename):
    if filename:
        print("reading", filename)
    else:
        raise ValueError("unable to read file")

section = 'global'
with open(filename) as fp:
    config.read_file(itertools.chain(['[{}]'.format(section)], fp), source = filename)

no_syslog = config[section]['no-syslog']
if no_syslog == 'yes':
    logfile = "/var/log/messages"
elif no_syslog == 'no':
    logfile = config[section]['logfile']

if logfile:
    mcelogPathFound = True

memoryErrors = readLogFile(logfile)

Upvotes: 1

iCart
iCart

Reputation: 2351

You can just split the line to get the value you want:

line.split(' = ')[1]

However, you might want to look at the documentation for configparser module.

Upvotes: 2

Related Questions