Python Forum
Sorting Text within a .txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Text within a .txt file
#6
So I came up with this following code, please bear with me if it is terrible. But its not giving me any syntax errors and seems to be running. When I re-open the text file to view the newly sorted results, the text file is empty. Is this because I am "w" and not "a"?

import itertools
def main():
    inputFile= open("students.txt", 'r')
    lineList = inputFile.readlines()
    with open("students.txt", "r") as text_file:
        for line in itertools.islice(text_file,0,2):
            my_tuple = (line)
            for n in my_tuple:
                sorted(my_tuple)
                inputFile.close()
                with open("students.txt", "w") as text_file:
                    text_file.write(my_tuple)
                    close.inputFile()
main()
Reply


Messages In This Thread
Sorting Text within a .txt file - by drogers10940 - Dec-15-2017, 10:32 PM
RE: Sorting Text within a .txt file - by squenson - Dec-15-2017, 10:43 PM
RE: Sorting Text within a .txt file - by wavic - Dec-15-2017, 11:16 PM
RE: Sorting Text within a .txt file - by wavic - Dec-15-2017, 11:30 PM
RE: Sorting Text within a .txt file - by drogers10940 - Dec-16-2017, 12:31 AM
RE: Sorting Text within a .txt file - by squenson - Dec-16-2017, 06:27 AM
RE: Sorting Text within a .txt file - by squenson - Dec-16-2017, 04:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert text from an image to a text file Evil_Patrick 5 4,476 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,710 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Sorting File Miraclefruit 1 3,493 Apr-16-2017, 01:28 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020