tomSurge tomSurge - 1 year ago 74
Ini Question

Reading values as a list from ini file are picked at random

I have a problem reading some values from an ini file as a list. I have made a program which takes two lists as parameters (source, destination) and then copies the source paths to the destination paths. It gets these paths from an ini file called paths.ini. However, I noticed the values were not added in a chronological order, which meant that the first value in the ini file was not necessarily the first value in the list. This is a critical error, seeing as I want the first source to only be copied to the first destination.

Here is a sample code of what I have done:

src = list(dict(config.items('src')).values())
dst = list(dict(config.items('dst')).values())
print("src: "+ str(src) + " dst: "+ str(dst))

except Exception as e:
messagebox.showerror("Data mismatch", "Mismatch between data from ini and core file")

if(utilFunctions.copyto(src, dst, True)):

For those wanting to see the ini file:

src1 = C:/Users/saUser/Documents/copyExample/singleCopies/1/2/3.txt
src2 = C:/Users/saUser/Documents/copyExample/singleCopies/1/2/3.txt

dst1 = C:/Users/saUser/Documents/copyExample/singleCopies/1
dst2 = C:/Users/saUser/Documents/copyExample/singleCopies/1

Answer Source

I guess you are using the builtin ConfigParser. According to the documentation, that is using a collections.OrderedDict to store the values in your files, so while the read is made sequentially, it should be okay and you end up with an ordered dictionary representing your file contents from a [section] top to the bottom.

If you want a list from that, just don't do that list(dict(... thing you are doing. Better use a list comprehension:

src = [value for key, value in config.items('src')]
dst = [value for key, value in config.items('dst')]

Then you will have what you want, ordered as well, in your src and dst.