Another question from the 'Learn to Program...' book:

Dan Hamm dan at dmhamm.org
Wed Jan 16 01:33:25 EST 2002


I copied the code example from page 95 (no errata sheet data on this):



import os, time

try: # initialize files and variables

    menu = open('menu.txt','r')

    outf = open('menu.tmp','w')

    header = 'Menu for %s' %time.ctime(time.time())



    # write header and append menu body

    outf.write(header + '\n')

    lines = menu.readlines()

    for i in range(2,len(lines)):

        outf.write(lines[i])

    outf.close

    menu.close



    # manipulate files

    os.rename('menu.txt','menu.bak')

    os.rename('menu.tmp','menu.bak')

    os.remove('menu.bak')

except:

    print 'ERROR: Failed to create new menu'



But when I run it, it just prints out the error message without modifying
the file. The status bar says the module was reloaded successfully, but that
seems to be all that happens. Did I miss a step somewhere?



Dan






More information about the Python-list mailing list