I am writing a script that modifies any text files. It replaces white space lines with blank lines. It erases the blank lines at the end of the file. The image shows the output I want.
I am able to get very close to the desired output. The problem is that I cannot get rid of the last blank line. I think this has something to do with the last line. e.g ' the lines below me should be gone
actually looks like this ' the lines below me should be gone\n'
It looks like new lines are created on the previous line. e.g if line 4 has \n
than line 5 will actually be the blank line not line 4.
I should note that I can't use rstrip
or strip
My code so far.
def clean_file(filename):
# function to check if the line can be deleted
def is_all_whitespace(line):
for char in line:
if char != ' ' and char != '\n':
return False
return True
# generates the new lines
with open(filename, 'r') as file:
file_out = []
for line in file:
if is_all_whitespace(line):
line = '\n'
file_out.append(line)
# removes whitespaces at the end of file
while file_out[-1] == '\n': # while the last item in lst is blank
file_out.pop(-1) # removes last element
# writes the new the output to file
with open(filename, 'w') as file:
file.write(''.join(file_out))
clean_file('test.txt')
The \n
essentially means "create another line"
So when you've removed all the lines that are \n
, there's still the preceding line
the lines below me should be gone\n
Which again means "create another line", beyond the ones you've already removed
Since you say you can't use rstrip
, you could end the loop with
file_out[-1] = file_out[-1].strip('\n')
to remove \n
from the last element. Because \n
can't exist anywhere else in a line, rstrip
and strip
will have the same effect
Or without any strip
or endswith
:
if file_out[-1][-1] == '\n':
file_out[-1] = file_out[-1][:-1]
Note that \n
is a single character, ordinal 0x0a
as hex, not two characters \
and n
, ordinals 0x5c
and 0x6e
. That is why we use -1
and not -2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With