vendredi 17 juin 2016

Appending to a file, then reading from it into a list, then re-appending to it and overwriting certain parts

I want to be able to have a program whereby the user can input a paragraph/sentence/word/character whatever and have that stored in a list e.g. in list[0]. Then I want them to be able to write another bit of text and have that stored in e.g. list[1]. Then at any time I want the user to be able to read that from the list by choosing which segment they want to read from e.g. reading "hello" from list[0] whilst in list[1] "hi" is stored. Then when the user exits the program I want the list to be written to an external file. Then, at next start up, the program should read the file contents and store it again in the list so that the user can add more bits of text or read the current bits. When the list is saved to a file it should append new or changed parts but overwrite parts that are the same so as not to have duplicates. I have attempted this without much success. I am to be honest not sure if it is possible. I have browsed similar forums and have found that hasn't helped much so here it is.

My code so far:

    import os
    import time
    import csv

    global write_list
    global f1_contents
    write_list = []


    def write():
        os.system("cls")
        user_story = input("Enter your text: n")
        write_list.append(user_story)


    def read():
        os.system("cls")
        user_select_needs = True
        while user_select_needs == True:
            user_select = input("Enter the list section to read from or type exit: n")
            if user_select == "exit":
                user_select_needs = False
            try:
                int(user_select)
                select = user_select
                select = int(select)
                try:
                    print(write_list[select])
                    user_select_needs = False
                    enter = input("Press enter:")
                except:
                    print("There is not stored data on that section!")           
            except ValueError:
                print("That is not a valid section!")



    def exit():
        os.system("cls")
        max_num_needs = True
        while max_num_needs == True:
            set_max_num = input("Set the storage: n")
            try:
                int(set_max_num)
                max_num = set_max_num
                max_num = int(max_num)
                max_num_needs = False
            except:
                print("It must be an integer!")
        for i in range(0, max_num):
        f = open("Saves.txt", "a")
        f.write(write_list[i])
        f.close()
        os._exit(1)


    def main():
        store_num_needs = True
        while store_num_needs == True:
            set_store_num = input("State the current storage amount: n")
            try:
                int(set_store_num)
                store_num = set_store_num
                store_num = int(store_num)
                store_num_needs = False
            except:
                print("It must be an integer!")
        try:
            f1 = open("Saves.txt", "r")
            for i in range(0, store_num+1):
                i, = f1.split("#")
            f1.close()
        except:
            print("--------Loading-------")
            time.sleep(1)    
        while True:
            os.system("cls")
            user_choice = ""
            print("Main Menu" + "n" + "---------")
            print("1) Write")
            print("2) Read")
            print("3) Exit")
            while user_choice not in ["1", "2", "3"]:
                user_choice = input("Pick 1, 2 or 3 n")
            if user_choice == "1":
                write()
            elif user_choice == "2":
                read()
            else:
                exit()

    if __name__ == "__main__":
        main()

It might be too complicated to understand in which case just ask me in comments- otherwise general tips would be nice aswell.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire