Reputation: 21
I have some question about my code:
def entry_book():
book = ["autor", "ime", "godina", "ISNB", "zanr", "cena", "broj"]
print ("Podaci za knjigu:")
book[0] = input ("Autor: ")
book[1] = input ("Naslov: ")
book[2] = input ("Godina: ")
book[3] = input ("ISNB: ")
book[4] = input ("Zanr: ")
book[5] = input ("Cena: ")
book[6] = input ("Kolicina: ")
record= "{}|{}|{}|{}|{}|{}|{}".format (book[0], book[1], book[2], book[3],
book[4], book[5], book[6])
print (book)
print (record)
f = open('books.txt','w')
f.write (record)
f.close()
f = open("books.txt")
x = f.read()
f.close()
print (x)
record1 = record.split('|')
print (record1)
second_meni()
This is code to store information on books, which I want to access later (like at a library). However, every time I add/create a new book, the old one gets deleted. Can anyone help me rewrite the code so it can store the old data as well. Or please explain what is the correct way to do it?
Upvotes: 1
Views: 209
Reputation: 1208
I think there are two methods to do this:
FIRST
f = open('knjige.txt','w')
is the piece of code which is responsible for rewriting the existing data in your file. Other option which python offers to append some new data to the existing data is to open a file for writing using append 'a' method. So you can replace your above statement with
f = open('knjige.txt','a')
It won't replace the file with new data you enter.
SECOND
Other option is to open your file in read method, f = open('knjige.txt','r')
and copy the existing data to a variable ( variable=f.read('knjige.txt')
). You can also use pickle module and its functions dump and load if you need to maintain your datatype.
Now concatenate your new data to the values in 'variable' and again open the file in write method and write it back to it.
Upvotes: 1
Reputation: 15204
I took the liberty of pythonizing your code a bit.
def unos_knjiga():
headers = ["Autor", "Naslov", "Godina", "ISNB", "Zanr", "Cena", "Kolicina"]
print("Podaci za knjigu:")
knjiga = [input("{}".format(obj + ': ')) for obj in headers] # see 1
zapis = '|'.join(knjiga) # see 2
print(knjiga)
print(zapis)
with open('knjige.txt', 'a') as f: # see 3
f.write(zapis + '\n')
# i guess this is for testing?
with open("knjige.txt", 'r') as f:
x = f.read()
print(x)
# and this too?
zapis1 = zapis.split('|')
print(zapis1)
# this is not mentioned anywhere
second_meni()
1) This is a list comprehension. It creates lists by looping through stuff. In this case we are looping through the header
list and use its items to construct input
statements. The provided input is stored in the list.
2) .join()
method. It does what you explicitly did. Joins items from iterators using a string between them.
3) the with
keyword. Manages files so that you do not have to. Unless there is a reason not to use it, use it. This was also where the real problem with your code was. You have to use the 'a'
mode. 'a'
is for append, 'w'
is for write. In this context, write means delete everything that was there and write this new stuff. Also note that 'a'
mode can also create files, you do not need to temporarily switch to 'w'
for that ('r'
does not; 'r'
is for read).
Cheers!
Upvotes: 2
Reputation: 15357
You have to use
f = open('knjige.txt', 'a')
'w' recreates the file (so use it only for NEW files, or if you don't mind it will be overwritten, 'a' appends to a file.
See python open built-in function: difference between modes a, a+, w, w+, and r+?
Also some unrelated suggestions:
Upvotes: 6
Reputation: 6914
Your call to open the file, f = open('knjige.txt','w')
opens the file, truncating the existing contents should it exist. If you open the file with a mode that appends contents, like a
it should not delete previous lines. See https://docs.python.org/2/library/functions.html#open for more information on opening files for reading / writing.
Upvotes: 0