Re: how to remove the blank lines?

Discussion in 'Python' started by Peter Otten, Dec 8, 2006.

  1. Peter Otten

    Peter Otten Guest

    boyeestudio wrote:

    > The following program have the same function as the linux's command:tail
    > -f logfile.log
    > But it print a lot of blank lines while showing on the stdout.
    > How to fix this problem and let it go ahead always
    > showing the end line as the log file's increasing.
    >
    > #tail.py:
    >
    > import os
    > import sys
    >
    > class Tail:
    > def __init__(self,inputstream):
    > self.inputstream=inputstream
    >
    > def tail(self):
    > self.inputstream.seek(0,2) ???Maybe this is where I'll need
    > fixed,But I don't know how to....


    You probably want to move to the end of the file once at the
    beginning of the script, not before every attempt to read from it...

    > line=self.inputstream.readline().strip()


    Don't strip() so fast. You lose the information whether you have reached the
    (current) end of file or a blank line (readline() returns "" or "\n"
    respectively).

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


    That's always true, I think.

    > try:
    > t=Tail(arg)
    > t.tail()
    > except KeyboardInterrupt:
    > arg.close()
    > print "File closed"


    A no-fluff version might be

    filename = sys.argv[1]
    infile = open(filename, "r")
    infile.seek(0, 2)
    while True:
    while True:
    line = infile.readline()
    if not line: # an empty string indicates eof
    break
    print line,
    time.sleep(1) # give the writing program a chance to write more lines


    The inner loop can also be written

    for line in iter(infile.readline, ""):
    print line,

    Peter
     
    Peter Otten, Dec 8, 2006
    #1
    1. Advertising

  2. Peter Otten

    Peter Otten Guest

    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
     
    Peter Otten, Dec 9, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NNTP
    Replies:
    2
    Views:
    963
    rakesh sharma
    Apr 7, 2004
  2. Replies:
    7
    Views:
    501
    Peter J. Acklam
    Sep 12, 2005
  3. Jack Wang
    Replies:
    5
    Views:
    102
    Mario D'Alessio
    Feb 15, 2008
  4. Cah Sableng
    Replies:
    0
    Views:
    254
    Cah Sableng
    Apr 23, 2007
  5. Replies:
    6
    Views:
    148
    Thomas 'PointedEars' Lahn
    Jan 27, 2008
Loading...

Share This Page