[Tutor] os.listdir fn

Nandan bagchee at cse.ohio-state.edu
Sat Dec 11 06:32:39 CET 2004


> It does if you use the glob module :-)
>
> Python, with batteries included.
> But sometimes finding the right battery can be challenging...
>

Muttering 'globbing is a Perl concept, listing dirs must be in file ops' I
turned first to the Files section of the Nutshell book :-) But I came up with
this code, which I'm happy with for several reasons:

def createaproposjlist(opname):
    filelist=os.listdir('./icon.scripts/'+opname)
    spltnames=map(os.path.splitext,filelist)
    scripts=filter(lambda x: x[1]=='.script', spltnames)
    filenames=map(''.join,scripts)
    filebases=map(lambda x: x[0], spltnames)
    return filebases;

Cheers,
Nandan





More information about the Tutor mailing list