I have a code that takes a file and splits the file into two files. The problem I am having is when writing the lower half of the file I have two lines of whitespace I need removed. Here is what I have and am wonder how I could add a line of code to remove the extra lines.

Files=glob.glob('/home/mypathname/*.TAB')
output= ('/home/mypathname/')
for (count, aFileName) in enumerate(files):

    filename = aFileName.split('/')[11]
    day=aFileName.split('/')[10] 
    print day
    print filename
    F=open(aFileName, 'r')#.readlines() 

    _,keep=F.read().split('END ',1)
    #keep,_=F.read().split('END ',1)
    print keep 


    new=open(output+('/')+day+('/')+filename+('test'), 'w')

    for line in keep:

        new.write(line)
    #new.write("END")


    new.close()

new.write('\n'.join(keep)).rstrip('\n') (or .lstrip if in beginning) instead of lines 18-23 ?

I had tried something similar to that and obtained the same error i recieved using your suggested line of code.
AttributeError: 'NoneType' object has no attribute 'rstrip'

Sorry should be:

new.write('\n'.join(keep).rstrip('\n')) (or .lstrip if in beginning) instead of lines 18-23 ?

For some reason with that line is striping off everything and writing a blank file.

Test for length after you strip the line, and note that "new" is already used by Python.

for line in keep:
    if len(line.strip()):
        new_fp.write(line)

for line in keep:

if len(line.strip()):

new_fp.write(line)

This unfortually changes the structure of the lines in my file and I need to keep the structure just remove the blank lines before the actually context of the file is wrote.

This article has been dead for over six months. Start a new discussion instead.