osprey
osprey

Reputation: 768

Set of filenames not sorted

I'm using os.walk with a generator to populate a set of filenames for later manipulation using the following:

file_path = '/home/user/Developer/10/'
list_of_files = {}
cnt = 0
for (dirpath, dirnames, filenames) in os.walk(file_path):
    for filename in filenames:
        if filename.endswith('.xml'):
            list_of_files[cnt] = os.sep.join( [dirpath, filename] )
            cnt += 1

With list_of_files sorted as:

{0: '/home/user/Developer/10/2/test/channe 1_UTC_DEtoSE_183126.585.xml',
 1: '/home/user/Developer/10/2/test/channe 1_UTC_DEtoSE_183216.572.xml',
 2: '/home/user/Developer/10/2/test/channe 1_UTC_DEtoSE_183123.015.xml',
 3: '/home/user/Developer/10/2/test/channe 1_UTC_DEtoSE_183058.016.xml',
 4: '/home/user/Developer/10/2/test/channe 1_UTC_DEtoSE_183130.151.xml',
 5: '/home/user/Developer/10/2/test/channe 1_UTC_DEtoSE_183140.873.xml',
 6: '/home/user/Developer/10/2/test/channe 1_UTC_DEtoSE_183223.729.xml',
 7: '/home/user/Developer/10/2/test/channe 1_UTC_DEtoSE_183054.451.xml',
 8: '/home/user/Developer/10/2/test/channe 1_UTC_DEtoSE_183148.014.xml',
 9: '/home/user/Developer/10/2/test/channe 1_UTC_DEtoSE_183202.296.xml'}

I know that python does not sort filenames when populating lists, but I was under the impression that sets were self-sorting? If not how can I sort this set alphanumerically by filename? If I use sorted() if returns a list object with set element numbers which is pretty useless.

Upvotes: 0

Views: 45

Answers (3)

park
park

Reputation: 11

I'm not sure if snippet below would be of some help; basically, you sort by the dictionary's values (not key).

for v in sorted(list_of_files.values()):
    print(v)

Upvotes: 1

Nk03
Nk03

Reputation: 14949

you can use the pathlib module to extract all the XML files with just one dict comprehension.

from pathlib import Path

file_path = Path('./home/user/Developer/10/')
list_of_files = {
    index: str(xml_file.absolute())
    for index, xml_file in enumerate(file_path.glob('**/*.xml'))
}

list_of_files  = dict(sorted(list_of_files.items(),key=lambda x:x[0])) # sort dict based on values

Upvotes: 0

osprey
osprey

Reputation: 768

As comments pointed out, list_of_files is a dictionary, not a set. Changing list_of_files to be initialized as an empty list and using append() such as:

file_path = '/home/user/Developer/10/'
list_of_files = []
for (dirpath, dirnames, filenames) in os.walk(file_path):
    for filename in filenames:
        if filename.endswith('.xml'):
            list_of_files.append( os.sep.join( [dirpath, filename] ) )

does the trick.

Thanks for the helpful, timely comments!

Upvotes: 0

Related Questions