★ wanayoo — archive 1999 http://mail.python.org/pipermail/tutor/2001-September/008903.htmlNouvelle recherche | Portail wanayoo

[Tutor] poplib questions

alan.gauld@bt.com alan.gauld@bt.com
Fri, 21 Sep 2001 13:20:11 +0100


> Where should I open and close the file?  Open (for append) 
> just before the "for i in range" statement and close after 
> "a.quit()" as I have it below?

I'd leave it pretty much as is - definitely open outside 
any loops! But also add a try/finally block and call close 
inside the finally  section:


>    if numMsgs == 0:
>       print "\nNo mail for "+USER+" at "+MAILSERVER
>       return

coz you do a return here you don't really need the else:


>    else:
     
     try:
>       print "Retrieving "+`numMsgs`+" messages for "+USER+" 
> at "+MAILSERVER
>       f=open("testbox","a")
>       for i in range(1, numMsgs + 1):
>            (header, msg, octets) = a.retr(i)
>            print "Message ", `i`, "("+`octets`+" octets)", ':'
>            for line in msg:
>                    f.write('   ' + line +"\n")
>                    print ".",
> #            dele(i)
> #           f.write('\n-----------------------\n')
>        a.quit()
     finally:
	>    f.close()
>    return

Just a thought.

Alan G.