Just read the file as-is and then write the output with the lines that aren't blank. Here's an example:
# Read lines as a list
fh = open("myfile", "r")
lines = fh.readlines()
fh.close()
# Weed out blank lines with filter
lines = filter(lambda x: not x.isspace(), lines)
# Write
fh = open("output", "w")
fh.write("".join(lines))
# should also work instead of joining the list:
# fh.writelines(lines)
fh.close() Also, if you don't understand my lambda expression there, here's a longer route instead of using filter :
fh = open("myfile", "r")
lines = fh.readlines()
fh.close()
keep = []
for line in lines:
if not line.isspace():
keep.append(line)
fh = open("output", "w")
fh.write("".join(keep))
# should also work instead of joining the list:
# fh.writelines(keep)
fh.close() All this is untested as I'm at work! But this should give you an idea of how to do it.
Hope that helps!
shadwickman
Posting Pro in Training
497 posts since Jul 2007
Reputation Points: 186
Solved Threads: 77