Subject: Re: [Tutor] os.listdir fn

Nandan bagchee at cse.ohio-state.edu
Mon Dec 13 03:55:58 CET 2004



> Message-ID: <41BAEB1B.1090906 at tds.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Are you sure this does what you want? You compute 'scripts' and 'filenames' and throw them away. I
> think this function will return the base name of all the files *and folders* in ./icon.scripts/opname.

I meant to keep the x.script version, but realised I could append '.script' to the basename anytime.

> If you are trying to return the base name of every file or directory whose extension is '.script',
> you can use a list comprehension to filter the list:
>
> def createaproposjlist(opname):
>      filelist=os.listdir('./icon.scripts/'+opname)
>      spltnames=map(os.path.splitext,filelist)
>      return [ name for name, ext in spltnames if ext == '.script' ]

The list comp is much nicer! thanks.

> If you are concerned about directories with names ending in '.script' then add another filter using
> os.path.isdir:

No, I'm keeping a list of icons and associated scripts in the directory, and I know there
won't be any subdirs. But I'll keep isdir in mind.

Cheers,
Nandan



More information about the Tutor mailing list