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

Chris Brault groundhog3000 at yahoo.com
Sat Sep 10 11:04:46 CDT 2005


After some corrections,

I've tested the following code and it works well:


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

import os, os.path, re, sys

# 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) and
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 re.findall("\S", line) == []:
         print line
         print "LENGTH:", len(line), "FINDALL:",
re.findall("\S", line)
         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()

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


Gabe



	
		
______________________________________________________
Click here to donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/




More information about the Christiansource mailing list