Python - remove empty lines of text at the end of a file

I am writing a script that modifies any text files. It replaces white spaces with blank lines. It erases empty lines at the end of the file. The image shows the result I want.

enter image description here

I can get closer to the desired result. The problem is that I cannot get rid of the last empty line. I think this has something to do with the last line. for example, ' the lines below me should be gone looks like this: ' the lines below me should be gone\n' It seems that new lines are created in the previous line. for example, if line 4 has \n than line 5, it will actually be an empty line, not line 4.

I should note that I cannot use rstrip or strip

My code is still.

 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') 
+8
python
source share
1 answer

\n essentially means "create another line"

So, when you deleted all lines \n , there is still the previous line

 the lines below me should be gone\n 

Which again means β€œcreate another line”, beyond the ones you have already deleted

Since you say you cannot use rstrip , you can end the loop with

 file_out[-1] = file_out[-1].strip('\n') 

remove \n from the last item. Since \n cannot exist anywhere on the 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, serial number 0x0a as hexadecimal, not two characters \ and n , ordinals 0x5c and 0x6e . That's why we use -1 , not -2

+6
source share

All Articles