Jeff, Thanks for all the advice on good python coding. I'm digesting it right now fiddling with tempfiles.
I'd come to the conclusion that my file changes weren't being changed, and had even written an inefficient loop to write lines back to a file. Except this didn't work too well, and there was a point where the del lines worked. Perhaps it wasn't supposed to. To be more correct, I should probably import webchecker.py as a module and invoke its functions/variables directly ... The os.system was a hack to get it working to begin with. I'll post version 2 when it's better .... > -----Original Message----- > From: Jeff Shannon [mailto:[EMAIL PROTECTED] > Sent: Thursday, March 06, 2003 10:42 AM > To: [EMAIL PROTECTED] > Cc: Adam Getchell > Subject: Re: Deleting line at the end of a file > > > Adam Getchell wrote: > > >Hi all, > > > >I have a utility that invokes webchecker on a number of different > >websites, given by a list, then mails the results to another > list. I'm > >trying to trim off the last line in the results file (per > invocation on > >a site), [...] > > > > I'm not exactly sure why you're getting the specific errors that you > showed, but I have some comments. > > ># loop through websites > >for site in websites: > > # Write headers for each site > > fp = open(outfile,'a') > > fp.write('\n' + site + '\n') > > fp.close() > > # Run the webchecker script and write results to file > > os.system("webchecker.py -q %s >> " %site + outfile ) > > # Trim off last line > > fp = open(outfile, 'r+') > > lines = fp.readlines() > > del lines[-1] > > fp.close() > > > > First, I'd put all of this into a function. It'll help to > organize your > code and make things a little more clear. Second, when you > build your > system command, you're using % formatting *and* string > addition -- why > not just use % for both? > > os.system("webchecker.py -q %s >> %s" % (site, outfile)) > > This would ensure that you're not having problems with > operator precedence. > > However, the most important point is that, when you read in the file > that webchecker has just created, you're not actually *doing* > anything > with the data. Even if 'del lines[-1]' didn't throw an exception, it > wouldn't accomplish what you want, because you never write > your changes > back to the file. > > ># Get local time > >runtime = strftime('%a %d %b %Y %H:%M:%S') > ># Create message > >fp = open(outfile,'r') > >msg = MIMEText(fp.read()) > >fp.close() > > > ># Loop through recipients > >for you in recipients: > > me = "Webnanny" > > msg['Subject'] = 'Sites link check %s' % runtime > > msg['From'] = me > > msg['To']= you > > > > server = smtplib.SMTP('hrrm.ucdavis.edu') > > server.set_debuglevel(1) > > #server.connect() > > server.sendmail(me, you, msg.as_string()) > >server.quit() > > > > Several of the parts inside your loop are going to be the > same each time > through, so those should be pulled up out of the loop. Also, keep in > mind that SMTP.sendmail() can fail or throw exceptions, and that you > want to close the connection even if that happens (and > probably that you > want to keep trying other email addresses if it fails before the last > one). That means you should use a try/finally block, something like > this (modified from code of my own): > > me = "Webnanny" > msg['From'] = me > msg['Subject'] = 'Sites link check %s' % runtime > server = smtplib.SMTP(SMTPServer) > try: > for you in recipientlist: > msg['To'] = you > try: > print server.sendmail(me, you, msg.as_string()) > except: > pass > finally: > server.quit() > > As far as addressing your original problem, I'd suggest a somewhat > different approach. Instead of having webchecker append > repeatedly to a > single file, and then trying to adjust that file's contents manually > between each invocation of webchecker, it'd be simpler to use the > tempfile module to create a new, temporary file for each > invocation of > webchecker. Then you read in each file in turn, and massage > your data > as you collect the file contents into a single list of lines. > That list > can then be join()ed and fed directly to MIMEText. This will > save you > from having any need to worry about append modes. (I suspect > that your > exceptions may be coming from reading from the end of the file.) > > Jeff Shannon > Technician/Programmer > Credit International > > _______________________________________________ ActivePython mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython