[Tutor] Reading matching text files in a folder

Remco Gerlich scarblac@pino.selwerd.nl
Tue, 30 Oct 2001 16:04:20 +0100


On  0, "Schmidt, Allen J." <aschmidt@nv.cc.va.us> wrote:
> I've been lurking for a while but now need some help...
> 
> I have a program that reads a hard-coded filename and path like
> "F:\\import\\20011030-Tue.txt"
> 
> It works and breaks each line up into parts, strips out apostrophes  and
> inserts the fields into a MySQL database table.
> 
> What I need to be able to do:
> Look into the above folder (F:\\import\\) and see if there are any *.txt
> files in it.

import os

files = os.listdir("F:\\import\\*.txt")

Gives a list of files.

> If so, pass it by name into my loop that breaks it up and does the DB
> insert.

for file in files:
   pass_into_his_loop(file)
   
> I also need access to the date (20011030) and the day (Tue) to use as fields
> in the insert.

filename = os.path.basename(file)

dash = filename.index('-')
dot = filename.index('.')

date = filename[:dash]
day = filename[dash+1:dot]


That's a very brute force and hard coded way, but if your files always look
like that it'll work ok. If you have an old Python you can use
string.index() or string.find() to find the index.

> Then when done, rename the file by adding some prefix and suffix so it is
> not processed again the next day.

Use os.rename() to rename files, eg os.rename(filename, filename+".DONE")

> AND...if there is more than one, loop through each of them in turn and
> perform the above needed actions.

That's what the loop above did.

> 
> Have not worked with files much and need some guidance. Links to examples
> also appreciated.

Hope this helps. The os, os.path and string modules should have everything
you need.

-- 
Remco Gerlich