little help

Psybar Phreak psybar_phreak at yahoo.com
Sun May 11 01:05:49 EDT 2003


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" ?

thanks!!


"Jordan Krushen" <jordan at krushen.com> wrote in message
news:oprozrwang5ctagx at shawnews...
> On Sun, 11 May 2003 14:23:03 +1000, Psybar Phreak
<psybar_phreak at yahoo.com>
> wrote:
>
> > im not getting a compile error - it just aint printing anything to
> > screen!
> >
> > WEIRD!
> >
> >> for diaryname in filesInDir:
> >> InFile = open(diaryname, 'r')
> >> lines = InFile.readlines()
> >> InFile.close()
> >>
> >> diaryTitle = lines[0].strip()
> >> diaryDesc = lines[1].strip()
>
> I hope I'm not being too obvious by pointing out that you don't have any
> 'print' statements written here.
>
> Additionally, you seem to be ignoring your 'dirpath' var here, as the
> open() call should have the directory prefixed to the diaryname (otherwise
> it tries to open it in the current directory).
>
> Try this (some small changes to your code):
>
> import os
>
> dirpath = '/some/dir/here'
>
> filesInDir = os.listdir(dirpath)
> filesInDir.sort()
>
> for diaryname in filesInDir:
>     InFile = open(dirpath + '/' + diaryname, 'r')
>     lines = InFile.readlines()
>     InFile.close()
>     diaryTitle = lines[0].strip()
>     diaryDesc = lines[1].strip()
>     print diaryname
>     print diaryTitle
>     print diaryDesc
>     print
>
> There are also functions to expand the full path to files, check out the
> os.path module.
>
> J.






More information about the Python-list mailing list