Python Forum
how to add the user input from file into list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to add the user input from file into list
#1
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.
Reply
#2
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".
Reply
#3
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")
    ...
Reply
#4
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])
Reply
#5
See post 3.
Please don't start new threads on same subject, it's against forum rules.
Reply
#6
Sorry, new to this forum, what do you mean by post 3?
Reply
#7
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.
Reply
#8
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()
Reply
#9
ou are not following my example.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,257 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Need help implementing an input with file operations Gaijin 3 2,016 Jun-08-2022, 05:50 PM
Last Post: Gaijin
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,841 May-14-2021, 07:14 AM
Last Post: perfringo
  Print user input into triangle djtjhokie 1 2,343 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 7,885 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,759 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,052 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 2,981 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,465 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 2,534 Mar-11-2019, 07:37 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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