0

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

DJR
  • 21
  • 1
  • 4
  • Why did you think it wouldn't? – jonrsharpe Sep 24 '15 at 14:43
  • I want something like: Read file : file01.ini... ['sectionf1'] Read file : file02.ini... ['sectionf2'] Read file : file03.ini... ['sectionf3', 'sectionf31'] Read file : file04.ini... ['sectionf4'] – DJR Sep 24 '15 at 14:50
  • To put it another way: if you want them separate, why reuse the same `ConfigParser`? Have you considered reading the documentation? – jonrsharpe Sep 24 '15 at 14:51
  • Yes i read the RTFM ;-)) but don't see/unterstand how to read in separate (..) i need to read many files, for each file, read section1...sectionxx and for each sectio read key/pair value, etc.. What is the good way ? – DJR Sep 24 '15 at 14:57
  • Use a separate parser for each one, move it inside the loop. – jonrsharpe Sep 24 '15 at 14:57
  • but, I do not know how many files in the directory, how to do this ? – DJR Sep 24 '15 at 15:01
  • ...why would that matter? The `for` loop doesn't care! This isn't a tutorial service; try something out and see what happens. – jonrsharpe Sep 24 '15 at 15:01

0 Answers0