configparser.configparser()从多个文件中读取sections()

时间:2015-09-24 14:39:32

标签: python python-3.x configparser

我的示例文件:

File01

[sectionf1]
Keyf1=Valf1

File02

[sectionf2]
Keyf2=Valf2

File03

[sectionf3]
Keyf3=Valf3

[sectionf31]
Keyf31=Valf31

File04

[sectionf4]
Keyf4=Valf4

代码(Python 3.2,Debian Wheezy 7.9):

import sys, os, shutil, configparser, datetime, re

cfgpars = configparser.ConfigParser()
cfgpars.optionxform = str
cfgpars._interpolation = configparser.ExtendedInterpolation()

#============ Handle the Python Path ===========#


pathname = os.path.dirname(sys.argv[0])
CDay= str(datetime.date.fromordinal(datetime.date.today().toordinal()))

PathFiles = os.path.abspath(pathname) + "/rbackup-files/"

dirs = os.listdir( PathFiles )

for File in dirs:
    cfgpars.read(PathFiles + File)
    print("\nRead file : " + File + "...")
    print (cfgpars.sections() )

print("\nDone !")

我的产品:

Read file : file01.ini...
['sectionf1']

Read file : file02.ini...
['sectionf1', 'sectionf2']

Read file : file03.ini...
['sectionf1', 'sectionf2', 'sectionf3', 'sectionf31']

Read file : file04.ini...
['sectionf1', 'sectionf2', 'sectionf3', 'sectionf31', 'sectionf4']

Done !

有什么问题?我不明白为什么cfgpars.sections()会添加所有部分。

通过http://httk.openmaterialsdb.se/_modules/httk/task/reader.html找到解决方案

for FileR in dirs:
    FilePath= PathFiles+FileR
    print("\nRead file : " + FileR + "..." + FilePath)
    cfgpars = configparser.ConfigParser()
    cfgpars.optionxform = str
    cfgpars._interpolation = configparser.ExtendedInterpolation()
    cfgpars.read([FilePath])
    print (cfgpars.sections() )

pas deremerciementàceuxquiugugèrentquestackoverflow n'est pas un tuto。 un petit试试这个auraitétébienvenue。 Gardons l'esprit ouvert et soyons bienveillants envers ceux qui cherchent(...)

不,谢谢那些建议stackoverflow不是教程的人。尝试这个小本来欢迎。保持开放的心态,善待那些寻求

的人

0 个答案:

没有答案
相关问题