-6
import os

books = open(os.path.expanduser("~/Desktop/books.txt")).read()
b= books.split('\n')
del b[-1]
book={}
for i in b:
    b1=i.split('\t')
    book[b1[0]]=[b1[1],b1[2],b1[3]]

def all_book():

    print "The Book List" 
    books = open(os.path.expanduser("~/Desktop/books.txt")) 
    print books.read()

def add_book():

    print "Registering New Book"
    books = open(os.path.expanduser("~/Desktop/books.txt"))
    name = raw_input("Title: ") 
    author= raw_input("Author Name: ") 
    publisher =raw_input("Publisher: ") 
    n= int(b1[0])
    n1 = n+1
    newb= [str(n1), '\t', name, '\t', author,'\t', publisher] 
    books.writelines(newb) #Adding file to the list
    newb = {}
    newb[n1]=[name, author, publisher] 
    print 'A New Book Added!' 
    return newb

def del_book():

    print "Deleting Books" 
    delnum = str(raw_input("Registered Number:"))
    if delnum in book:
        del book[delnum]
    else:
        print delnum, "Not Found"


def show_menu():

    print '''
    1) add new
    2) all show
    3) delete
    4) search
    5) Save/out
    '''
    menu_choice = raw_input('what --> ')
    if menu_choice == '1':
            add_book()
    elif menu_choice == '2':
            all_book()
    elif menu_choice == '3':
            del_book()

show_menu()
Doug Richardson
  • 10,483
  • 6
  • 51
  • 77
  • add_book() here is not working I tried for 2 hours and failed..ha newb= [str(n1), '\t', name, '\t', author,'\t', publisher] books.writelines(newb) #Adding file to the list – Naeun Kim Oct 11 '15 at 19:14
  • Please fix the indentation, tell us the error message if any and use a more descriptive title! – cdonts Oct 11 '15 at 19:20
  • @Naeun Kim : For having syntaxic colouring, indent each line with (extra) 4 spaces. Please be descriptive about what's not working. – Emmanuel DURIN Oct 11 '15 at 19:27

2 Answers2

1
books = open(os.path.expanduser("~/Desktop/books.txt")).read()

Your error lies here. If you do not specify a file opening mode, Python will default to 'read' mode, meaning you cannot write to it. The correct syntax for opening a file for writing is:

books = open('file', 'w')

This page has a table of file access modes down the page.

pppery
  • 3,731
  • 22
  • 33
  • 46
le_wofl
  • 304
  • 3
  • 15
0

open() default open the file as read, and that's why you can't write your new book into the text file.

user1537085
  • 404
  • 5
  • 18