Python Forum
List Won't Write in Text File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Won't Write in Text File
#5
And just for reference, this is how I imagined the solution. Main points are: to use the while loop instead of recursively calling add_to_lp_list() (the function calls itself - in most cases it's bad practice). Using globals is also a considered bad practice. Prefer passing arguments to functions instead (in the following code it's finalize_lp_list(lp_list), which calls def finalize_lp_list(unsorted_list)


import time


def add_to_lp_list():  #####This should loop until you decide you are ready
    add = "start"
    lp_list = []    #####This is not the final list, but a list where you add everything to be sorted

    while add != "done":
        print("Artist: Album (or type \"done\" if you have finished the list)")
        add = input()

        if add == "done":
            finalize_lp_list(lp_list)
        elif add == "":
            print("Input empty, discarding.")
        else:
            print("Is this correct? " + add)
            print("Press Enter to confirm, typing anything else will discard.")
            answer = input()

            if answer == "":
                lp_list.append(add)
            else:
                print("Discarding last.")


def finalize_lp_list(unsorted_list):
    lp_list_sorted = sorted(unsorted_list)  ####This list is the final product, with everything sorted

    lp_list_final = lp_list_sorted

    with open("LP_list.txt", 'w') as output_file:
        for item in lp_list_final:
            output_file.write(item + "\n\n")

    print("List is now in the text file.")
    time.sleep(10)

    exit()


####Actual

add_to_lp_list()
Reply


Messages In This Thread
List Won't Write in Text File - by IILawrenceII - Jul-17-2020, 08:40 PM
RE: List Won't Write in Text File - by j.crater - Jul-17-2020, 09:31 PM
RE: List Won't Write in Text File - by IILawrenceII - Jul-17-2020, 10:45 PM
RE: List Won't Write in Text File - by j.crater - Jul-17-2020, 10:55 PM
RE: List Won't Write in Text File - by j.crater - Jul-17-2020, 11:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What does .flush do? How can I change this to write to the file? Pedroski55 3 227 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 439 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,548 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,470 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 6,698 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,113 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,628 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,049 Jan-21-2023, 06:57 AM
Last Post: jacklee26
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,132 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  read a text file, find all integers, append to list oldtrafford 12 3,615 Aug-11-2022, 08:23 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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