Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 830481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:03:05+00:00 2026-05-15T04:03:05+00:00

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

  • 0

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 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

the only problem is that the text is displayed at the bottom of the text file everytime it’s sortened…

I assume it also sorts the blank lines…anybody knows why?

and maybe can you suggest some tips on how to avoid this happening?

thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:03:06+00:00Added an answer on May 15, 2026 at 4:03 am

    An “empty” line read from a text file is represented in Python by a string containing only a newline (“\n”). You may also want to avoid lines whose “data” consists only of spaces, tabs, etc (“whitespace”). The str.strip() method lets you detect both cases (a newline is whitespace).

    f = open("tracks.txt", "r")
    # omit empty lines and lines containing only whitespace
    lines = [line for line in f if line.strip()]
    f.close()
    lines.sort()
    # now write the output file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this small code library that I'm considering releasing into Open Source. I
I have an ActionScript 2.0 file that contains a small library. This library parses
I have this small jquery script that does not work if I remove the
Let's say I have a line of text like this Small 0.0..20.0 0.00 1.49
I have a small app that has a Render thread. All this thread does
I have a small script that moves the frame head backwards and forwards depending
I have this small PHP script: <?php session_start(); $var = array(); $var['key'] = 'Var
Heya guys, I have this small script and i need to grep all the
I have a small script that launches and, every half hour, feeds a command
Here I have a small VBS script that helps me append a new line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.