folder extraction

John Machin sjmachin at lexicon.net
Thu Jan 1 04:56:10 EST 2009


On Jan 1, 8:23 pm, r.gr... at science-computing.de wrote:
> On Dec 30 2008, 4:30 pm, ibpe... at gmail.com wrote:
>
> > how do i get along with this task of extracting multiples folder and
> > generating their names individually in a their respective files as
> > they were generated.
>
> Hallo,
> I hope, that I interpret your question in the right way.
> You can use the following function as a starting point to get all
> files ending with py or pyc from your working dir.
> Invoke getAllFilesOfPatterns(".","*.py *.pyc")
>
> import os
> import fnmatch
> def getAllFilesOfPatterns( dir ,patterns="*",  recursive=True  ):
>     """ patterns must be space separeted string of patterns
>         e.g: *.pdf *.ps *.html
>         """
>     patterns= patterns.split()
>     retValue=[]
>     for path,dirs,files in os.walk(dir):
>         for file in files:
>             for pattern in patterns:
>                 if fnmatch.fnmatch( file , pattern ):
>                     retValue.append(os.path.join(path,file))
# put a "break" after the append so that you avoid (a) duplicates [if
the patterns are not mutually exclusive] (b) waste of CPU time
[always]
>         if not recursive: break
>     return retValue
>
> Greetings




More information about the Python-list mailing list