condensed syntax?
Sean 'Shaleh' Perry
shalehperry at attbi.com
Tue Jul 30 11:00:53 EDT 2002
On 30-Jul-2002 Matthieu Bec wrote:
>
>
> is there any way to condense the following:
>
> # scan subdir(s) within mydir
> for d in os.listdir(mydir):
> dd=os.path.join(mydir,d)
> if os.path.isdir(dd):
> self.subdir.append(dd)
>
>
> what follows doesn't work, but I'm thinking of a something like:
>
> self.subdir=[(os.path.isdir(dd=os.path.join(mydir,d))?dd:pass) for d in
> os.listdir(mydir) ]
>
so we find a way to kill the join().
oldwd = os.getcwd()
os.chdir(mydir)
self.subdir = filter(os.path.isdir, os.listdir('.'))
os.chdir(oldwd)
I like to use filter here because it is concise and portable across python
versions.
More information about the Python-list
mailing list