Reputation: 1802
Goal:
I have a file named airplanes.txt
. It already contains some of my favourite planes:
Boeing 747
Airbus A380
I am taking an input in my main app in order to add a plane stated by the user into this airplanes.txt
file.
Example:
Please type the name of the plane you want to add to your favourites list:
<<Boeing 737>>
If you look at the airplanes.txt
file now you see:
Boeing 747
Airbus A380
Boeing 737
My question:
My code below works by first getting the existing data in the airplanes.txt
.
airplane_file = open('airplanes.txt', 'r')
favourite_airplanes = [ line.strip('\n') for line in airplane_file.readlines()]
airplane_file.close()
Next, I close the file, and then, take a user input.
add_plane = input('Enter The Airplane You Would Like To Add To This List: ')
Finally, I write back all the existing data which was in the file and then add on the user input to that.
if add_plane != None:
reopened_airplane_file = open('airplanes.txt', 'r+')
for airplane in favourite_airplanes:
reopened_airplane_file.write(f'{airplane}\n')
else:
reopened_airplane_file.write(f'{add_plane}\n')
I feel like there should be some way to write data into existing files which contain data without needing to rewrite all the data back into it + new data.
This can become increasingly tedious as, for enormous files, it would take a huge amount to time to write only the existing data back into the file and then write the new data which you would expect to add.
I have tried to make this question as readable as possible by deconstructing my code so I hope you don't mind the long question. :)
Edit:
This worked for me:
with open('airplanes.txt', 'a+') as file:
add_plane = input('Enter The Airplane You Would Like To Add To This List: ')
file.write(f'{add_plane}\n')
Why it wasn't working:
I was using the r+
mode which meant that I was overwriting though a+
mode was the appropriate one to use!
a+
open for reading and appending (writing at end of file). The file is created if it does not exist.
Upvotes: 3
Views: 96
Reputation:
with open('airplanes.txt', 'a+') as f:
fav_air = f.readlines()
planes_list = [i.strip() for i in fav_air]
add_plane = input('Enter The Airplane You Would Like To Add To This List: ')
for plane in planes_list:
if add_plane != plane:
f.write("\n")
# Append text at the end of file
f.write(plane)
else:
print(" Airplane already in the list")
Upvotes: 1
Reputation: 46
with open("airplanes.txt", "a+") as ap_txt:
add_plane = input('Enter The Airplane You Would Like To Add To This List: ')
ap_txt.write("\n{}".format(add_plane))
Upvotes: 1
Reputation: 333
You can read and write/append to the file at the same time, so here is how it's gonna work in your case:
with open('airplanes.txt', 'a+') as airplane_file:
favourite_airplanes = [line.strip('\n') for line in airplane_file.readlines()]
add_plane = input('Enter The Airplane You Would Like To Add To This List: ')
if add_plane != None:
airplane_file.write(f'{add_plane}\n')
Upvotes: 1
Reputation: 43169
You can open files in different modes, that is read them, write to them or append to them. This is done via the second parameter "r", "w", "a", optionally with "+" or "b" for "byte mode". Additionally, use with open(...)
instead.
That said, you might use:
with open("test.txt", "a+") as file:
file.write("\nBoeing 737")
Upvotes: 2