[Tutor] Reading matching text files in a folder

Lloyd Kvam lkvam@venix.com
Tue, 30 Oct 2001 09:55:58 -0500


Some code snippets to help:
>>> flist = os.popen("dir *.py /B").readlines()
>>> flist
['class_test.py\012', 'fib.py\012', 'fnx_counts.py\012', 'ia_test.py\012', 'lloyd_util.py\012', 'localhost_8000.py\012', 'parkAP.py\012', 'parkCGI.py\012', 'parkCOM.py\012', 'park_test.py\012', 'pos.py\012', 'resv.py\012', 'sqrt_sum.py\012',
'testdict.py\012', 'testresv.py\012', 'three_n_plus_one.py\012', 'ui_dict.py\012', 'util.py\012']
>>> import DateTime
>>> xdate = DateTime.Date(2001,3,15)	# the ides of March, 2001
>>> xdate.day
15
>>> xdate.day_of_week
3

Programming Python (Lutz) devotes chapter 2 to covering System Tools: reading directories and processing files.

DateTime is from mxDateTime a very complet package of date and time handling tools.

"Schmidt, Allen J." 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.
> If so, pass it by name into my loop that breaks it up and does the DB
> insert.
> I also need access to the date (20011030) and the day (Tue) to use as fields
> in the insert.
> Then when done, rename the file by adding some prefix and suffix so it is
> not processed again the next day.
> 
> AND...if there is more than one, loop through each of them in turn and
> perform the above needed actions.
> 
> Have not worked with files much and need some guidance. Links to examples
> also appreciated.
> 
> Thanks
> 
> Allen
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582