[Tutor] Adjusting a file
shey crompton
shey@argonaut.com
Mon, 26 Aug 2002 15:39:31 +0100
I have a file called Menu.txt located in C:\Python22 on my PC. I am trying
to open the file, add a header, and then close it again. There's only one
problem, when I run the script below, I get a syntax error the line after
the except statement.
I copied the script straight from How to Program Using Python.
Incidentally, the script returned the same error message when I was just
using 'Menu.txt' for the file as opposed to C:/Python22...
Thanks for any help/tips.
Shey
import os, time
try:
menu=open('C:/Python22/Menu.txt','r')
outf=open('C:/Python22/Menu.txt','w')
header='Menu for %s' % time.ctime(time.time())
outf.write(header + '\n')
lines = menu.readlines()
for i in range(2,len(lines)):
outf.write(lines[i])
outf.close()
menu.close()
os.rename('C:/Python22/Menu.txt', 'C:/Python22/Menu.bak')
os.rename('C:/Python22/Menu.tmp', 'C:/Python22/Menu.txt')
os.remove('C:/Python22/Menu.bak')
except:
print 'ERROR: Failed to create new menu'