blank lines in file after sorting content of a text file in python

后端 未结 3 1184
北恋
北恋 2020-12-31 11:41

I have this small script that sorts the content of a text file

# The built-in function `open` opens a file and returns a file object.

# Read mode opens a fi         


        
3条回答
  •  礼貌的吻别
    2020-12-31 12:12

    The reason it sorts the blank lines is that they are there. A blank line is an empty string followed by \n (or \r\n or \r, depending on the OS). Perfectly sortable.

    I should like to note that "try:" nested into a "try:... except" block is a bit ugly, and I'd close the file after reading, for style's sake.

提交回复
热议问题