parse text files in a directory?

Discussion in 'Python' started by jo3c, Jan 2, 2008.

  1. jo3c

    jo3c Guest

    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.

    thanks in advance

    joe
    jo3c, Jan 2, 2008
    #1
    1. Advertising

  2. jo3c

    Tim Chase Guest

    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
    Tim Chase, Jan 2, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. crazyprakash
    Replies:
    4
    Views:
    3,370
    adrian
    Oct 30, 2005
  2. Replies:
    4
    Views:
    949
    M.E.Farmer
    Feb 13, 2005
  3. Replies:
    19
    Views:
    1,118
    Daniel Vallstrom
    Mar 15, 2005
  4. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    183
    7stud --
    Feb 20, 2008
  5. Replies:
    17
    Views:
    171
    Henry Law
    Dec 15, 2007
Loading...

Share This Page