python - Remove last elf from file -


I want to remove eol (the end of the line and empty rows) from a file I

problem The input file is ending with text and EOL I am trying to delete EOL from the file after writing a text file as a list of words, many lines may end with an empty line or \ n

Input file

  It is testing the word list with returns in the word list  

Source code

  Open file name = "input_file.txt" (file name, 'r') as FN form: fn For file in line: = '\ n': x = line print x, with opening # (file name, 'w') foo: # foo.write (x)  

Thanks to any suggestions and discussion, the whole content is more effective than strip - it does not keep the whole file contents in memory - so it will be fine for large files This code does the blank lines buffers and when we open the blank line If not, then only prints them, so, in the last blank lines Drit will not.

open with open_empty_lines = 0 (file name, 'r') for the line in fn: fn: if line! = '\ N': For i xrange (prev_empty_lines): print '\ n', prev_emply_lines = 0 print line, other: prev_empty_lines + = 1

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -