[Tutor] open multiple files

Dave Kuhlman dkuhlman at rexx.com
Thu Aug 16 00:50:00 CEST 2007


On Wed, Aug 15, 2007 at 08:26:45AM -0700, Paulo Quaglio wrote:
> Hi everyone - 

>   I'm beginning to learn how to program in python. I need to
>   process several text files simultaneously. The program needs to
>   open several files (like a corpus) and output the total number
>   of words. I can do that with just one file but not the whole
>   directory. I tried glob but it didn't work. Your ideas are
>   greatly appreciated. Thanks,

Here is a pattern that you might use (if I understand your problem
correctly):

    import glob

    names = glob.glob('*.txt')
    for name in names:
        infile = open(name, 'r')
           etc. etc
        infile.close()


Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list