Skip to content Skip to sidebar Skip to footer

Python: Parsing And Grouping Filenames In Directory

I'm pretty new to python, but I have lots of experience with MATLAB & C. What I need to do it parse the filenames of files in a particular directory, separate them into groups

Solution 1:

You can use defaultdict to make a dictionary that contains lists:

from collections import defaultdict

groups = defaultdict(list)

for filename in os.listdir(directory):
    basename, extension = os.path.splitext(filename)
    project, subject, session, ftype = basename.split('-x-')

    groups[session].append(filename)

Now, groups contains a mapping between session names and filenames.

Solution 2:

How about using a defaultdict to group filenames, glob to find the appropriate files, and fileinput to read lines from all files with the same key. (untested)

import os
from glob import glob
import fileinput
from collections import defaultdict

filenames = glob('*-x-*')
dd = defaultdict(list)
for filename in filenames:
    name, ext = os.path.splitext(filename)
    dd[tuple(name.split('-x-')[:3])].append(filename)

for key, fnames in dd.iteritems():
     for line in fileinput.FileInput(fnames):
         pass# do something with lines from files with same key

Post a Comment for "Python: Parsing And Grouping Filenames In Directory"