Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   parse text files in a directory? (http://www.velocityreviews.com/forums/t565789-parse-text-files-in-a-directory.html)

jo3c 01-02-2008 02:32 AM

parse text files in a directory?
 
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,H2008010 5.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

Tim Chase 01-02-2008 03:32 AM

Re: parse text files in a directory?
 
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,H2008010 5.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








All times are GMT. The time now is 06:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.