Skip to main content

Using Python's FileList class

·285 words·2 mins

Recently I was looking for a Python class that would let me easily store and manipulate a list of file paths. I pretty quickly found distutils.filelist, which sounded like exactly what I needed, so I read the docs… and came away with no clear understanding of how to actually USE the class. Furthermore, Googling turned up nothing either.

The good news is that the class is quite compact. (Here is the source.) And since it’s so simple, I was quickly able to work out how to use it. The key thing to know is that there are two variables: allfiles and files. The first contains everything, and you fill it up either using findall() or set_allfiles(). Once you have some files in allfiles, you can include them in the reduced list of files via include_pattern(). After you’ve selected some items, just read the files property.

If instead you only want to remove files/paths matching a pattern, you can put items into files directly and then cull the ones you don’t want with exclude_pattern(). Of course, you can also combine the two methods.

So this is the FileList example that the docs should contain, but don’t:

import distutils.filelist as fl

fileList = fl.FileList()
fileList.findall() # By default, uses CWD
fileList.include_pattern('*.xib', anchor=False)

print fileList.files

This will recursively look through all the files in the current directory and pull out the .xibs. What if the files aren’t on disk, but instead have come from a zip file? No problem:

import distutils.filelist as fl

fileList = fl.FileList()
zipfile = ZipFile(StringIO(someZipData))

fileList.extend(zipfile.namelist()) # Extends 'files', not 'allfiles'
fileList.exclude_pattern('__MACOSX/*') # Ignore OS X attribute directories
fileList.exclude_pattern('', prefix='.') # Ignore hidden files

print fileList.files</pre>

I hope these examples save you some time!