title: Looking for a way to list various image sequences in a directory
body: <p>I have a directory with some image sequences that follow a similar naming convention, for example: test.1001.exr, test.1002.exr, test.1001.dpx, test.1002.dpx.</p>
  <p>I'm trying to write some code that would output the following:</p>
  <p>test.1001-1002.exr</p>
  <p>test02.1001-1002.dpx</p>
  <p>I found an example which does list them in the way I would like, however it is not very robust and would not show the file extension unless I print the specific one.</p>
  <pre><code>if os.path.exists(scanDir) is True:
      scans = []
  
      for root, dirnames, filenames in os.walk(scanDir):
          for file in filenames:
              for ext in fileFormats:
                  if ext in file:
                      scans.append(file)
  
      scans = [os.path.splitext(file)[0] for file in scans]
      scans.sort()
  
      def extract_number(name):
          return re.findall(r&quot;\d+$&quot;, name)[0]
  
      def collapse_group(group):
          if len(group) == 1:
              return group[0][1]  # Unique names collapse to themselves.
          first = extract_number(group[0][1])  # Fetch range
          last = extract_number(group[-1][1])  # of this group.
          length = len(str(int(last)))
          return &quot;%s%s-%s%s&quot; % (group[0][1][:-length],
              first[-length:], last[-length:], &quot;.dpx&quot;)
  
      groups = [collapse_group(tuple(group)) \
          for key, group in itertools.groupby(enumerate(scans),
              lambda index_name: index_name[0] - int(extract_number(index_name[1])))]
  else:
      groups = []
  </code></pre>
  <p>I'm not sure I'm able to modify the code to achieve my result. Does anyone know a way I can do this?</p>