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

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

On Wed, 7 Apr 2004, Scot W. Stevenson wrote:

> Part of a Python program I'm writing uses the os.listdir("myfolder")
> call to get a list of the files in a folder (Python 2.3.3, Mac OS X
> 10.3.3).  Suddenly, without me doing anything I'm aware of, I have a
> file
> 	.DS_Store
> in my directory, which screws up all kinds of things. I can't see how
> Python is doing this (but I'm writing here just to be sure), and Mac OS
> X shouldn't be running around inserting files into my Python folders,
> either

Hi Scot,

Actually, it _is_ Mac OS X running around inserting files.

The '.DS_Store' file contains information that the Mac OS X Finder uses to
store the folder's state (like window position, the type of view being
used, etc...)

> , and, finally, I'm not happy about os.listdir() finding hidden files
> without me explicitly telling it to. That is, uh, sort of the reason for
> having them hidden...

os.listdir() is just being too truthful, then.  *grin*

Files that begin with a dot in Unix aren't really hidden: they're just not
displayed by default by the standard 'ls' file listing command (and other
utilities that follow the leading-dot-should-be-invisible convention.)

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.

Hope this helps!

More information about the Tutor mailing list