Home > Enterprise >  python Remembering the list for another use
python Remembering the list for another use

Time:11-24

suppose we have a main.py file and a_file.py that has a list

like this :

main.py

from a_file import *

while true:
    example = input("Enter Something : ")

    a_list.append(example)

    if example == 'showlist':

        print(a_list)


a_file.py

a_list = []

so as you can see the main.py file has a input that whatever you type in it gets stored in the a_list list in a_file.py

when you first want to run the main.py

it will ask some input in a loop and whatever you type gets appended to the a_list in the a_file.py

Here is the problem...

i want whatever you type in the input get stored in the list permanently

because when you close the python script and run it again , the list will be empty

so i want that everything that gets stored in the list permanently be in the list

so good luck helping me.. Thanks for reading my problem

CodePudding user response:

instead of putting the data in a list try using file handling methods to save your input in a permanent file like:

file = open("FILE PATH\\FILE NAME.txt", "w")
while True:
    example = input("Enter Something : ")

    file.write(example)

    if example == 'showlist':

        for line in file:
            print(line)

and by the way......try putting the if condition before adding the data to the file and making the adding in the else statement so that when you type "showlist" in the input you don't find it in the file. but the code above is just what you wanted, Hope that helps you.

CodePudding user response:

You need to save the list to a separate file and every time you start the program, you first have to read from that file and fill the list with all entries that are saved in the file.

There are multiple ways to do this. One would be to write the entries of a list to a text file, as long as the list will contain only strings, not arbitrary objects. You could do it like this:

import os
# For convenience I have moved the list to the same file
# It's not necessary though
a_list = []

savefile = 'data.txt'

# Repopulate the list with the entries in the file if it exists
if os.path.exists(savefile):
    with open(savefile, 'r') as f:
        a_list = [line.rstrip('\n') for line in f.readlines()]

while True:
    example = input("Enter Something : ")
    
    # It would be a good idea to introduce a break condition
    # This is just a suggestion on how to do it
    if example == 'stop loop':
        break

    a_list.append(example)
    
    # Check if file already exists
    if not os.path.exists(savefile):
        # If it doesn't exist, create it
        with open(savefile, 'w') as f:
            f.write(example   '\n')
    else:
        # If it exists, append to it
        with open(savefile, 'a') as f:
            f.write(example   '\n')

    if example == 'showlist':
        print(a_list)

Another way would be to serialize the list using pickle. Here, we store the whole list at once when the program terminates, so here you definitely need a break condition. On program start, we load the list from the pickle file.

import os
import pickle

a_list = []

savefile = 'data.pickle'

if os.path.exists(savefile):
    with open(savefile, 'rb') as f:
        a_list = pickle.load(f)

while True:
    example = input("Enter Something : ")

    if example == 'stop loop':
        with open(savefile, 'wb') as f:
            pickle.dump(a_list, f)
        break

    a_list.append(example)

    if example == 'showlist':
        print(a_list)
  • Related