Python 2.7 Output from text files without last blank line
Question: I've started to learn Python and I stucked on one task - I have 10 text files
and I am trying to write from them two outputs: Output 1 should look like
folder and name of file header folder and name of file header ...

Output 2 should look like folder and name of file | text | text | text folder and name of file | text | text | text ...

Although I looked throught many of questions, I have not found, how to delete
(or not write) last blank row - there should be always only text. All
solutions I tried (tell(), rstrip(), ...) deleted all signs for new line, so I
had file like folder and name of file headerfolder and name of file header.
Ufortunatelly because of the task definition I am allowed to use only glob and
re, so helpful sys is forbidden for me :(

I would really appreciate your help with that, this task is for me really
challenging but now I do not know how to continue :)

Thanks for any advice and have a nice day ;)

Code I am using:

    
    
        import re, glob
        file_list = glob.glob('./input/*txt')
        for file_name in file_list:
        input_file = open(file_name, 'r')
        output_1 = open('file_1', 'a')
        output_2 = open('file_2', 'a')
        for line in input_file:
        if re.search(r'\s{2,}\S{4,}\s{1}\S+:.*', line):
                        output_2.write(file_name.replace('.txt','|') + line)
                if re.search(r'\s{3,}\S{3,16}\s+X?\s[A-Z]{3,4}\d?\s+\d{1,3}.*', line):
                        field = re.findall('\S{3,16}\s{3,}', line) + re.findall('\s{2}\d{1,3}.*', line)
                        field_join = '|'.join(field)
                        field_clear = re.sub(r'(\s){2,}', '', field_join)
                        field_list = re.sub(' ', '|', field_clear, 1)
                        output_1.write(file_name.replace('.txt','|') + field_list + '\n')
        output_2.close()
        output_1.close()
        input_file.close()
    


Answer: At the beginning of the loop just `continue` (immediately go to next loop
iteration without executing any more code) if it's a blank line:

    
    
    for line in input_file:
        if not line.strip():
            continue
        # etc
    

EDIT: For only the last line if it's blank:

    
    
    input_file = input_file.readlines()
    last_iter = len(input_file) - 1
    for idx, line in enumerate(input_file):
        if idx == last_iter and not line.strip():
            break
    

Or before the loop:

    
    
    input_file = input_file.readlines()
    if not input_file[-1].strip():
        input_file = input_file[:-1]
    

