[Tutor] Python suddenly finding ".DS_Store" files in folder [use helper functions]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Apr 7 22:15:17 EDT 2004

On Thu, 8 Apr 2004, Scot W. Stevenson wrote:

> > Actually, it _is_ Mac OS X running around inserting files.
> Ye Gods. How strange. They certainly don't tell you that in the switcher
> ads, do they ...

Hi Scot,

The page:


is one place that talks about them in a little more detail.  Actually,
there appears to be a lot of controversy about the implementation of the
'.DS_Store' mechanism in the Finder; I googled for it, and the first thing
that comes up is a program to remove '.DS_Store' files.  *grin*

> > And if we want our programs to follow the same conventions, we'll have
> > to filter out those filenames ourselves.  If you write a small helper
> > function that filters out os.listdir()'s output, that should do the
> > trick.
> Yeah, now I have an ugly hack that checks if the file is .DS_Store and
> if so, skips it.

It shouldn't have to be ugly, if we add one level of indirection:

def mylistdir(directory):
    """A specialized version of os.listdir() that ignores files that
    start with a leading period."""
    filelist = os.listdir(directory)
    return [x for x in filelist
            if not (x.startswith('.'))]

We can use 'mylistdir()' function instead of os.listdir().  By writing a
helper function, we can limit the scope of the change to, effectively, a
few lines.

Hope this helps!

More information about the Tutor mailing list