我正在使用配置解析器解析配置文件我需要在单独的终端中的文件部分中拖尾每个文件我在mac上使用python
我编写的代码获取了文件部分中的所有内容,我只需要路径,并且需要在单独的子进程中尾随每一个
import ConfigParser
import os
def ConfigSectionMap(section):
dict1 = {}
options = Config.options(section)
for option in options:
try:
dict1[option] = Config.get(section, option)
if dict1[option] == -1:
DebugPrint("skip: %s" % option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1
Config = ConfigParser.ConfigParser()
Config.read("/etc/harvest.conf")
print Config.sections()
print ConfigSectionMap("files")
示例配置文件是
[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
mysql.err = /var/log/mysql.err
syslog.err = /var/log/syslog.err
答案 0 :(得分:1)
由于tail
接受多个文件,您可以依赖它:
from ConfigParser import ConfigParser
from subprocess import Popen
config = ConfigParser()
config.read('/etc/harvest.conf')
filenames = [value for name, value in config.items('files')]
process = Popen(['tail', '-f'] + filenames)
process.communicate()