title: blank lines in file after sorting content of a text file in python
body: <p>I have this small script that sorts the content of a text file</p>
  
  <pre><code># The built-in function `open` opens a file and returns a file object.
  
  # Read mode opens a file for reading only.
  try:
      f = open("tracks.txt", "r")
  
  
      try:
          # Read the entire contents of a file at once.
         # string = f.read() 
          # OR read one line at a time.
          #line = f.readline()
          # OR read all the lines into a list.
          lines = f.readlines()
          lines.sort()
          f.close()
          f = open('tracks.txt', 'w')
          f.writelines(lines) # Write a sequence of strings to a file
      finally:
          f.close()
  except IOError:
      pass
  </code></pre>
  
  <p>the only problem is that the text is displayed at the bottom of the text file everytime  it's sortened...<p>
  I assume it also sorts the blank lines...anybody knows why?<p>
  and maybe can you suggest some tips on how to avoid this happening?<p>
  thanks in advance</p>