Looping through files in a directory

fuglyducky fuglyducky at gmail.com
Thu Nov 11 17:09:23 CET 2010


On Nov 10, 4:14 pm, James Mills <prolo... at shortcircuit.net.au> wrote:
> On Thu, Nov 11, 2010 at 8:46 AM, Matty Sarro <msa... at gmail.com> wrote:
> > Short story - I have a few thousand files in a directory I need to parse
> > through. Is there a simple way to loop through files? I'd like to avoid
> > writing a python script that can parse 1 file, and have to call it a few
> > thousand times from a bash script. Any input or pointers to functions that'd
> > help would be very much appreciated. Thanks!
>
> os.walk or os.listdir
>
> cheers
> James
>
> --
> -- James Mills
> --
> -- "Problems are solved by method"

I was able to do something similar like this...

############################
rootPath = 'T:/'
pattern = '*.xml'

for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        full_path = (os.path.join(root, filename))
        print(full_path)
############################



More information about the Python-list mailing list