-1

I have a problem every time i try and save the text i write for "ime" or "autor" to an external text file. Any suggestions on how to solve this problem so i can store the info in an organized "category" like manner would be greatly appreciated.

def unosenje_knjiga():
    file = open("2.rtd", "a")
    ime = str(input("Ime knjige:"))
    while len(ime) <= 3:
        print("Molimo Vas unesite ime knjige ponovo!")
        ime = str(input("Ime knjige:"))

    autor = str(input("Autor knjige:"))
    while len(autor) <= 0:
        print("Molimo Vas unesite ime autora ponovo!")
        ime = str(input("Autor knjige:"))

    isbn = str(input("ISBN knjige:"))
    while len(isbn) <= 0:
        print("Molimo Vas unesite ISBN knjige ponovo!")
        ime = str(input("ISBN knjige:"))
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555

2 Answers2

0

You can write a string s to an opened file by file.write(s)

One simple format to store your data would be Comma Separated Values (CSV).

So all you need to do it join the three strings together and write them to the file:

s = '"%s","%s","%s"' % (ime,autor,isbn)
file.write(s + "\n")

You might want to fix your two while loops. Your second query always sets variable ime instead of autor/isbn.

snow
  • 438
  • 3
  • 7
0
  1. you can not use ime = str(input("Ime knjige:"));

instead use ime = raw_input("Ime knjige:"); because if you use ime = input("...") python tries to interpret the '...' as a valid python expression

as example, type in a shell

     str = input("enter input")

as input type 5+4, then

      print str

the result will be 9 because if you use input the content of the input is evaluated

  1. if you want to write something to a file you have to open a handle to the file then write/read to/from it and when finished close the file handle (search for 'python file input output')

    #!/usr/bin/python

    # Open a file

    fo = open("foo.txt", "wb") //binary file io

    fo.write( "Python is a great language.\nYeah its great!!\n");

    # Close opened file

    fo.close()

see https://www.tutorialspoint.com/python/python_files_io.htm

ralf htp
  • 9,149
  • 4
  • 22
  • 34