I am trying to read-in a file, and then write it taking out all the "blank" lines. I don't mean strip the lines of all white space. i want to keep all indentation. i just want to get rid of the blank lines in between the lines of text. I was going to read the string with a loop and take out all the blank lines but i got a bit stumped on exactly how to define a blank line. You shouldnt have to loop a variable char by char to determine this.. right? It seems like it would be a "\n" with no characters before or after it. basically a "\n" alone, but so far i haven't been able to define this. so i've deferred to trying to do it on a line by line basis when reading and writing the file. The file needs to maintain the indentation integrity because it will not run otherwise.
lines = []
with open(file_name, mode='r+', encoding='utf-8') as f:
for line in f:
if len(line) > 2:
lines.append(line)
print(len(line))
for line in f:
f.write((str(line) for line in lines))
i only invoked the print function to tell me how many characters were in a line as it is read in, and it seems the blank lines only read as 1 char.("\n" i assumed?) so i tried to only append anything > 1. So far it just writes it back to the file exactly how it is read in. i'm not 100 on my use of the write method as it is shown here either. Ive just taken up python about 10 days ago, and i'm sure i am doing something wrong, that it's most likely really easy.