little help

Jordan Krushen jordan at krushen.com
Sun May 11 02:09:35 EDT 2003


On Sun, 11 May 2003 15:05:49 +1000, Psybar Phreak <psybar_phreak at yahoo.com> 
wrote:

> ok
>
> that worked, kinda.
>
> prob is, in the directory, i have a folder - and after it processes the
> diary files, it tries to process the folder as well.
>
> how do i edit this, so it only processes files that end in ".txt" ?

glob.glob() is your friend.
See http://python.org/doc/current/lib/module-glob.html

Here's a slightly refactored version of your code using glob().  I've 
removed some unnecessary variable assignments.

Note that the default mode for open() is 'r', so I've ommitted it.  
Additionally, if you have Python 2.2, open() is now being replaced by 
file(), so you could replace that as well.

===
import glob

for diaryName in glob.glob('/some/dir/here/*.txt'):
    lines = open(diaryName).readlines()
    diaryTitle = lines[0].strip()
    diaryDesc = lines[1].strip()
    print diaryTitle
    print diaryDesc
    print ===

HTH,

J.




More information about the Python-list mailing list