Question: I’m trying to make a phonebook where I enter in values and insert them into a text file. I’m having trouble getting values to write under previous values using newline in text files.
First of all, you probably want to append, not write (which overwrites any existing text) to the text file (
open("file", "a")). You can just write that entire
user_input string to your file. Your
read_file function just needs to add each line to the list of users (in the example below, I made each user a list by splitting each line wherever there are commas). Also, your
write_file function seems redundant… Another thing, it’s better not to use global variables if it can be avoided.
#-----phonebook.txt-------- phonebook_file = "phonebook.txt" #-----input variables-------- f_name = input("What is your first name?") l_name = input("What is your last name?") address = input("What is your address?") city = input("What city do you live in?") state = input("What state are you living in?") phone_num = input("What is your phone number?") email = input("What is your email address?") user_input = f_name + "," + l_name + "," + address + "," + city + "," + state + "," + phone_num + "," + email print(user_input) #-----inserting user input to phonebook.txt-------- def insert_input(): # "a" is to append (add) rather than overwrite (delete all and then add new) open_file = open(phonebook_file, "a") open_file.write("\n" + user_input) open_file.close() #-----reading text file-------- def read_file(): users =  open_file = open(phonebook_file, "r") for line in open_file: users.append(line.split(",")) open_file.close() return users #-----functions call-------- insert_input() print(read_file())
To clarify, in the line
open_file = open(phonebook_file,"w"), there are a few things that could be improved.
This is unnecessary, as the variable is just containing a string. Though it more relates the the line mentioned above, and doesn’t actually affect the code, I’d say it’s better practice to use the raw string. Instead of setting the variable to
"phonebook.txt"and using it, you can just do this:
open("phonebook.txt","w"). In other words, you only need to provide the string. If you are going to make a function that takes in a file name and opens the file, then you’d want a variable, but if the variable is defined and won’t be changed, use the contents of the variable when possible.
The function open() has a few modes:
"r" - Prepares the file for reading.
"x" - Creates a file.
"w" - Deletes existing content in the file and then prepares the file for writing. (this is called overwriting) It will also create the given file if it doesn’t already exist.
"a" - Prepares the file for writing. The differents between this and
"w" is that
"a" simply adds to the end of the file, while
"w" writes over existing content.
With all this in mind, our final product would be:
open_file = open("phonebook.txt", "a").
While there are more changes that could be made, changing that line I mentioned at the beginning of the code to this should solve most (if not all) of your problems.
Hopefully this helps you, good luck!
Is there anyway to do this code without the “a” mode? I was instructed by my teacher to get the input list to write into text files by looping through a list. I probably should’ve been more specific in my original post. Got close but got confused on what to do next.
I think that the
a is something obligatory!
There’s no way to get it without the “a” mode, sadly.
No, you definitely could, it’s just overcomplicating things. Append mode is the mode that best suits this situation, to write instead of append would be adding needless difficulty.
Use your own original code, but repeatedly ask for input, like
while True: ... # Take user input and insert into file here
So you’re actually creating a list. Select all the lines then hit Tab for quick indenting. And just replace the
w with an