I am trying to write a program, that takes "source code" text file, and checks it line-by-line for more than two blank lines preceding a code line. I consider that line containing only whitespaces is blank.
My solution is to use global variable as a counter for preceding blank lines:
import os
import re
blank_lines_counter = 0
def check_blank_lines(line):
global blank_lines_counter
if re.match('^\s*$', line):
blank_lines_counter += 1
else:
if blank_lines_counter > 2:
blank_lines_counter = 0
return True
blank_lines_counter = 0
return False
with open('input_file.py', 'r', encoding='utf-8') as f:
lines = f.readlines()
for i, line in enumerate(lines, 1):
if check_blank_lines(line):
print(f'Line {i}: More than two blank lines used before this line')
But I don't like it because of global variable. Is there any way to rewrite this code in more fancy way and not using global variables, but still in procedural programming paradigm (not OOP)?