Python Forum

Full Version: how to add the user input from file into list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
May i know what code can i use to save user's input in my current list and save it permanently? For example,
letters=['A', 'B', 'C' ]
add_letter = input('Enter letter u want to add:')
How can i save the add_letter in my letters list permanently?
I am not looking for append as when i restart my program, it will not save the user input.
You need to write your user inputs to a file then load that list from that file whenever you start. Research "How to write to file in python".
basic file save:
mylist = []
filename = 'myfilename' 

with open(filename, 'w') as fp:
    fp.write(mylist)
to reload:
mylist = []
filename = 'myfilename' 

with open(filename as fp:
    mylist = fp.read()
you may want to see if file exists before reloading as it won't be there first time
from os import path

mylist = []
filename = 'myfilename' 

if path.exists(filename):
    with open(filename as fp:
        mylist = fp.read()
else:
    print(f"{filename} does not exist")
    ...
First, i get an input from user .
Then, i save the input in file because i need store it permanently.
Is it possible to add that input back to my list in my module?

for example:
letters=['A', 'B', 'C' ]
#in my file i already key in ABC
file = open('file.txt','a')
#assume user input is D
add_letter = input('Enter letter u want to add:')
file.close()
file = open('file.txt','r')
#will be printing ABCD
print (file.read())
file.close()
is it possible for me to add it back to my list so that user can find it?
for example :
#i wish to print the user input from the list(assume that you restart your program)
print(letters[-1])
See post 3.
Please don't start new threads on same subject, it's against forum rules.
Sorry, new to this forum, what do you mean by post 3?
post have a number in upper right hand corner, that is the post number
FYI: you can right click --> copy link location to get post url.
Yet, i still cant solve my problem. i am doing library system for my mini it project. for now, i need to create a subsystem to enable librarian to add books.
What code can i use so that i can put the books,author,and id that librarian had input back to my list.(means how can i append and save it permanently)
because when i write to txt file, all my data in list will combine together with no space between them.
Sorry for bothering u all because i am just studying foundation currently.
import list1

def add_books():
    file_b = open('file_b.txt','a')
    file_a = open('file_a.txt','a')
    file_i = open('file_i.txt','a')

    book_list , author_list , book_id_list = list1.all_list()

    add_book = input('Enter your book title : ')
    add_author = input('Enter the author : ')
    add_id = input('Enter your book id : ')

    file_b.write(add_book)
    file_a.write(add_author)
    file_i.write(add_id)

    file_b.close()
    file_a.close()
    file_i.close()

    file_b = open('file_b.txt','r')
    file_a = open('file_a.txt','r')
    file_i = open('file_i.txt','r')

    
    print(file_b.read())
    print(file_a.read())
    print(file_i.read())

    file_b.close()
    file_a.close()
    file_i.close()
    

add_books()
ou are not following my example.