Something keeps nibbling on my list
michael
mhyoung at valdosta.edu
Wed Apr 18 14:34:28 EDT 2001
My program reads a file and copies it into a list, line by
line. Then
writes the list to another file. for some reason the first 2
lines from
the first file are missing from the second file. i know that
they are
being read into the list because i print the whole list out
and they are
there. Can someone please help? The entire program is below.
TIA
Michael
################ Start program #######################
#!/usr/local/bin/python
#This program reads the hosts.deny file, removes duplicate entries and
#sorts them. It then sends the output to another file.
#This program requires Python 2.0.
import sys
import string
FILENAME = "/etc/hosts.deny" #The file the input is read from.
TEMPFILE = "./hosts.deny.test" #The file the output is written to.
EXCLUDE='' #Lines beginning with this should not be printed.
def get_line() :
buf = f.readline()
if (buf and buf[0] == EXCLUDE):
return 0
else:
return buf
def is_this_a_dup(buf, l) :
if (l and buf):
results = l.count(buf)
if (results > 0):
return l
else:
l.append(buf)
return l
else:
l = buf
return l
f = open(FILENAME, "r")
l = ["1"] #This is just to avoid the "NameError: There is no
# variable named 'l'". It can be taken care of later.
while 1:
buf = get_line()
if (buf == ''):
break
if (buf == 0): # If 0 is returned then the line read on this loop
begins
continue # with the EXCLUDE char and is not to be used.
else:
# sys.stdout.write(buf)
l = is_this_a_dup(buf, l)
print l
l.remove("1") #Removes the 1 that we put into the list at the
beginning of
# the program.
l.sort()
for i in l:
sys.stdout.write(i)
g = open(TEMPFILE, "w")
g.writelines(l)
g.close()
f.close()
More information about the Python-list
mailing list