add - Adding an x number of new lines to the end of a file Python -


i have multiple files looping over. if individual file doesn't have length want mask lines in file isn't used shape remains same calculations need in future. far code have:

pattern = "c:\users\name\folder\july\mid_2014-07-2?-00.csv" bad_files=[] filename in glob.iglob(patterm):     num_lines = sum(1 line in open(filename))     if num_lines!=144:         s = 144 - num_lines         bad_files.append(filename) print bad_files   

this far returns list of of files don't have 144 lines in them. want if file doesn't have 144 lines in (for example has 127 lines) want add 's' number of blank lines end of file make have 144 lines. want mask of lines in file shape maintained want lines won't used in calculations. there way add statement in loop adds 's' number of blank lines end of file , statement masks every line in bad file? there easier way achieve want should consider?

if don't need list of file names processing, can collapse to:

for filename in glob.iglob(pattern):     open(filename, 'r+a') fd:         num_lines, line in enumerate(fd): pass         # num_lines 1 *less* actual line count.         fd.write('\n' * max(0, 144 - num_lines)) 

note cannot filename.write() because file name string. write works on file descriptor.