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' ]
If you are concerned about directories with names ending in '.script' then add another filter using os.path.isdir:
def createaproposjlist(opname): basedir = './icon.scripts/'+opname filelist=os.listdir(basedir) filelist = [ f for f in filelist if os.path.isfile(os.path.join(basedir, f)) ] spltnames=map(os.path.splitext,filelist) return [ name for name, ext in spltnames if ext == '.script' ]
Kent
Nandan wrote:
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
_______________________________________________ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor