parse text files in a directory?

Tim Chase python.list at tim.thechases.com
Tue Jan 1 22:32:59 EST 2008


jo3c wrote:
> hi everybody
> im a newbie in python, i have a question
> 
> how do u parse a bunch of text files in a directory?
> 
> directory: /dir
> files: H20080101.txt ,
> H20080102.txt,H20080103.txt,H20080104.txt,H20080105.txt etc......
> 
> i already got a python script to read and insert a single text files
> into a postgres db.
> 
> is there anyway i can do it in a batch, cause i got like 2000 txt
> files.

>>> import os
>>> for filename in os.path.listdir(directory):
...    if is_interesting(filename):
...        do_something(filename)

you'll have to implement is_interesting() to catch filenames
you're interested in and you've already implemented the
do_something() body.

You might want to look at the "glob" module which allows for easy
filespec testing and name expansion for your "H*.txt" format:

  for filename in glob.glob('H*.txt'):
     do_something(filename)

If they're in subdirectories, you may want to investigate the
os.walk() generator which will walk the directory tree and allow
you to do something with the files:

  for path, dirs, files in os.walk(directory):
     for filename in files:
        filename = os.sep.join(path, filename)
        if is_interesting(filename):
            do_something(filename)

-tkc









More information about the Python-list mailing list