find -name *.ext in Python

Benoit Dupire bdupire at seatech.fau.edu
Fri Jul 14 09:57:06 EDT 2000


I wrote a simple Python script to perform the Unix function
find -name *.ext
on a NT system.
I would like to know if there is a way to simplify it (or to perfect
it).

--------------------------------------------------
usage : search <path> <extension>
Ex: search.extension('e:','doc')
--------------------------------------------------

import os, sys
from stat import *
from string import lower

_ext=""
_output=sys.stdout

def extension(dir,ext):
    """ usage: : search <path> <extension>\nEx:
search.extension('e:','doc')"""
    global _ext, _output
    _ext=lower(ext)
    _output=open("c:\\logdoc.txt","w")
    _search(dir)
    _output.close()

def _search(dir):
     print dir
 for f in os.listdir(dir):
  pathname ="%s\\%s" %(dir,f)
# pathname=os.path.join(dir,os.sep,f) doesn't work.....
  mode = os.stat(pathname)[ST_MODE]
              if S_ISDIR(mode):
               _search(pathname)
              elif S_ISREG(mode):
               if lower(pathname[-3:])==_ext:
                _output.write("%s\n" %(pathname[:-4]))





More information about the Python-list mailing list