0

I am creating a simple basic code as I am gonna use it for learning GUI. So i thought of creating a library sort of code. Its incomplete as this is a main error. Here is the code for it:

total_books = open('Total_books.txt', 'a+')

def lend(numeber_of_books, num_days, code_of_book):
    price_per_book = 1
    price_per_day = 2
    total_price = 0
    total_price = price_per_book * numeber_of_books
    total_price += numeber_of_books * (price_per_day * num_days)
    print(total_price, "is the cost.")
    y = code_of_book + "\n"
    lend_books.write(y)
    total_books.seek(0)
    lend_books.seek(0)
    lines = total_books.readlines()
    print(total_books.readlines())
    temp = open('total_books.txt', 'w+')
    for x in lines:
        if x == code_of_book + "\n":
            pass
        else:
            x.append(temp)
    temp.close()

lend(1, 3, "ASFD")

lend_books.close()
total_books.close()

Well, in the start of the file there are two files. You can put anything in them for now. As whatsoever the total_books.readlines() returns []. As you can see I tried various stuff.

Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45
GjdGr8
  • 133
  • 1
  • 1
  • 7
  • Do you guys need to know whats going on as my code is quite messy. – GjdGr8 Jun 17 '20 at 03:31
  • (1) are you sure that `total_books.txt` isn't empty to start with? (2) opening the file in mode `a+` places the cursor at the end, and ignores `.seek()` (see [this answer](https://stackoverflow.com/questions/1466000/difference-between-modes-a-a-w-w-and-r-in-built-in-open-function/30566011)), which leads to being unable to read anything once something has been written to it (3) Have you tried opening the file twice, separately, once for reading what's _already_ there and once for appending to that? – Green Cloak Guy Jun 17 '20 at 03:40
  • 1
    @Green that only says writes ignore seek, not reads. – Brian McCutchon Jun 17 '20 at 03:42
  • What is your *question*? – Karl Knechtel Jun 17 '20 at 03:46
  • I got the problem, I had to add seek(0) before doing readlines() thanks for the help – GjdGr8 Jun 17 '20 at 04:04

1 Answers1

0
total_books = open('Total_books.txt', 'a+')

def lend(numeber_of_books, num_days, code_of_book):
    ...
    lines = total_books.readlines()
    print(total_books.readlines())
    temp = open('total_books.txt', 'w+')
    for x in lines:
        if x == code_of_book + "\n":
            pass
        else:
            x.append(temp)
    temp.close()

lend(1, 3, "ASFD")
total_books.close()

So you open Total_books.txt in append mode; then call lend.

In lend you readlines and assign to lines. You then read it again - and attempt to print that. But you are already at the end, so the print shows nothing.

Now you open total_books.txt again, in a write/overwrite mode.

The rest of the function does some something with lines and x, but doesn't return anything. So we don't know if lines had anything useful.

And, I don't see any attempt to write to total_books.txt.

So between the double readlines, no return, no write, the double open, this code is both confusing, and does nothing useful.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • In the for function, the book that is lent is getting removed from total books and is getting written in the lent books file. – GjdGr8 Jun 17 '20 at 05:04