如何在mac机器上使用python打开子进程来跟踪配置文件中的文件?

2024-05-12 23:43:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在mac上使用python跟踪配置文件中的文件

我可以从配置文件中获取值,但无法打开相同的子进程

示例配置文件

[section1]

host_prefix = true

timestamp_prefix = true

[section2]

host = localhost

port = 1463

pids = /var/run/harvester

[files]

apache.access = /var/log/apache2/access.log

apache.errors = /var/log/apache2/errors.log

mail = /var/log/mail.log

mysql.log = /var/log/mysql.log

我正在打开配置文件并尝试获取文件路径,我需要在不同终端中的新子进程中跟踪它们

    #! /bin/env python
import StringIO
import os
import re
from multiprocessing import Process
COMMENT_CHAR = '#'
OPTION_CHAR =  '='

def parse_config(filename):
    options = {}
    f = open(filename)
    for line in f:
        if COMMENT_CHAR in line:
           line, comment = line.split(COMMENT_CHAR, 1)
        if OPTION_CHAR in line:
            option, value = line.split(OPTION_CHAR, 1)
            option = option.strip()
            value = value.strip()
            options[option] = value
    f.close()
    return options

try:
    f = open("/etc/harvest.conf", 'r')
    print 'found'
    options = parse_config('/etc/harvest.conf')
    print options.values()
    os.system('tail -f options.values')
except:
        try: 
            f = open("/usr/local/etc/harvest.conf", 'r')
            print 'found'
            options = parse_config('/usr/local/etc/harvest.conf')
            print options.values()
            os.system('tail -f options.values')
        except IOError:
            print 'cannot find file'

上面的代码给出了配置文件中包含“localhost”、“1463”的所有值 但是我只需要文件中的路径,并且需要在单独的子进程中跟踪它们


Tags: 文件importlogvaluevarconf配置文件line