My example files:
File01
[sectionf1]
Keyf1=Valf1
File02
[sectionf2]
Keyf2=Valf2
File03
[sectionf3]
Keyf3=Valf3
[sectionf31]
Keyf31=Valf31
File04
[sectionf4]
Keyf4=Valf4
Code (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 !")
My ouput :
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 !
What is wrong? I don't understand why cfgpars.sections()
adds all sections.
Found soluce via 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 de remerciement à ceux qui suggèrent que stackoverflow n'est pas un tuto. un petit try this aurait été bienvenue. Gardons l'esprit ouvert et soyons bienveillants envers ceux qui cherchent (...)
no thanks to those who suggest that stackoverflow is not a tutorial. try this small would have been welcome. Keep an open mind and be kind to those who seek