[CS-FSLUG] An easy way to remove blank lines?

Chris Brault groundhog3000 at yahoo.com
Fri Sep 9 22:02:31 CDT 2005


Here it is in a python file,

Let's name the file:  line_remover.py

#--------------------------------------------------
#!/usr/bin/python

import os, os.path

# First we find the file name from the input
file_name = sys.argv[1]

# If the file exist, then we extract the lines from it
if os.path.isfile(file_name) &&
os.path.exists(file_name):
    file_reference = file(file_name, 'r')
    file_lines = file_reference.readlines()
    file_reference.close()
    new_lines = file_lines

# Run through the lines of the file and erase blank
ones
for line in file_lines:
    if len(line) == 0 ||
line.findall("^*[\t\n\r\f\v]*$") == []:
       new_lines.remove(line)

# Rewrite the file without the blank lines
file_reference = file(file_name, 'w')
file_reference.writelines(new_lines)
file_reference.close()
   

#---------------------------------------------------

There it is in easy to understand, straight forward
python.
Not the most optimized code, but easy to read.

Gabe


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 




More information about the Christiansource mailing list