little help

Jordan Krushen jordan at krushen.com
Sun May 11 00:53:42 EDT 2003


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