0

I want to search for map names in maps. To make it more clear what I want, this is the map structure:

\All data
   \Submap1
      \SubSubmap1
         \some files
      \Subsubmap2

   \Submap2
   \Submap3

What I want to do is search for SubSubmap's. I want to search them on the name of the subsubmap.

I hope you guys can give me a head start, cause I can't find any way to search on the name of a map.

Jayyzz
  • 5
  • 1
  • How do you store this in python? – xiº Oct 05 '15 at 09:34
  • For other programs I used a path, and went trough all the files with a for loop. This isn't possible for maps as far as I know. Especially if I need to go into those maps for the submaps. All those maps and data is locally stored. – Jayyzz Oct 05 '15 at 09:43
  • I would say you need 'os.walk'. – xbello Oct 05 '15 at 09:51
  • I have read allot about os.walk indeed. But as far as I can find, you can only go trough Submaps'. Not the SubSubmaps (as far as I know at least). If I am wrong, please do correct me and show me how :) – Jayyzz Oct 05 '15 at 09:58
  • Do you want to get a content of Subsub.. folders? Or just count them? – xiº Oct 05 '15 at 10:06
  • I just want to confirm the titels. So I want to search for the subsub folder named "Hello_world" for example. If it is found, I want to search for the next folder name. If it isn't found, I'll put the name in a list and print this list at the end of the program. An addition to the map structure, Submap1 is called "Hello", subsubmap1 is "Hello_world" and subsubmap2 is called "Hello_chat" So all the Submaps have a part of the name of the subsubmaps in it. – Jayyzz Oct 05 '15 at 10:09
  • So it is always 2nd sublevel (subsubmpap) or arbitrary? – xiº Oct 05 '15 at 10:13
  • It is always 2nd sublevel. – Jayyzz Oct 05 '15 at 10:14

1 Answers1

0

Let's use explore() function from here to save result of os.walk() into dictionary.

And after that just iterate over names and match them with pattern.

My folders:

.\all_data
.\all_data\sub1
.\all_data\sub1\subsub1
.\all_data\sub1\subsub1\some_files
.\all_data\sub1\subsub2
.\all_data\sub2

def explore(starting_path):
  alld = {'': {}}

  for dirpath, dirnames, filenames in os.walk(starting_path):
    d = alld
    dirpath = dirpath[len(starting_path):]
    for subd in dirpath.split(os.sep):
      based = d
      d = d[subd]
    if dirnames:
      for dn in dirnames:
        d[dn] = {}
    else:
      based[subd] = filenames
  return alld['']

data = explore('.')

for k, v in data['all_data'].iteritems():
    if v:
        for key in v:
            if 'subsub' in key:
                print key

>>> {'all_data': {'sub1': {'subsub1': {'some_files': []}, 'subsub2': []},
          'sub2': []}}
>>> subsub2
>>> subsub1

You could use more smarter verifications here if 'subsub' in key: as regex and so on.

Community
  • 1
  • 1
xiº
  • 4,605
  • 3
  • 28
  • 39
  • Awesome, works as a charm ^^. Thanks for the great help :). – Jayyzz Oct 05 '15 at 11:01
  • I try'd to iterate over a list of searchterms with this code, only then everything gets multiplied with the amount of items in my list. How can I still iterate over my list of searchterms (replacing the 'subsub' in "if 'subsub in key') – Jayyzz Oct 05 '15 at 14:57