Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: how to remove the blank lines?

Reply
Thread Tools

Re: how to remove the blank lines?

 
 
Peter Otten
Guest
Posts: n/a
 
      12-08-2006
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

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-09-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
blank CD-R and blank DVD-R not recognized by Vista 64 Ultimate =?Utf-8?B?R3JlZyBLaXJrcGF0cmljaw==?= Windows 64bit 13 11-07-2007 12:23 PM
Need to remove blank XMLNS attribute from xmldocument =?Utf-8?B?RXdhbiBGYWlyd2VhdGhlcg==?= ASP .Net 0 04-14-2006 06:06 PM
Re: Remove a line if next line is blank Alan Connor Perl 0 05-07-2005 09:16 PM



Advertisments