how to remove the blank lines?

Peter Otten __peter__ at web.de
Sat Dec 9 02:43:58 EST 2006


boyeestudio wrote:

Please keep the discourse on c.l.py/python-list.

> Thanks a lot!
> I have modified it,and it works well now!
> but the KeyboardInterrupt on which I have a little problem.
> When I type CTRL+C,it show me an I/O error.
> it seems that even as I click the CTRL+C,the try block is to be invoked at
> the same time!
>
> import os
> import sys
> import time
>
> class Tail:
>     def __init__(self,inputstream):
>         self.inputstream=inputstream
>         self.inputstream.seek(0,2)
>
>     def tail(self):
>         line=self.inputstream.read().strip()
>         if not line:
>             return
>         print line

This may skip whitespace in the inputstream, probably not what you want.

> if __name__=="__main__":
>     if len(sys.argv)<=1:
>         print "You must type a file name"
>         sys.exit()
>     arg=file(sys.argv[1],'r+')
>     t=Tail(arg)

As I told you, arg is always True. Change

>     while(arg):

to 
      while True:

to make it obvious.

>         try:
>             t.tail()
>             time.sleep(0.75)
>         except KeyboardInterrupt:
>             print "File closing"
>             time.sleep(1)
>             arg.close()

Put a
              break

into the except clause, or you will never leave the loop in a controlled
manner. Alternatively, move the try ... except out ouf the loop:

try:
    while True:
        t.tail()
        time.sleep(0.75)
except KeyboardInterrupt:
    pass
arg.close()
              
Peter



More information about the Python-list mailing list