[Tutor] Adjusting a file
shey crompton
shey@argonaut.com
Mon, 26 Aug 2002 17:53:50 +0100
Thanks Alan. The outf line should have been writing to Menu.tmp. When I
changed the suffix to .tmp everything ran correctly.
One of these days I'll learn how to read! :-)
-----Original Message-----
From: alan.gauld@bt.com [mailto:alan.gauld@bt.com]
Sent: 26 August 2002 17:13
To: shey@argonaut.com; tutor@python.org
Subject: RE: [Tutor] Adjusting a file
> problem, when I run the script below, I get a syntax error
> the line after the except statement.
Not sure what that is but I think you have another problem...
> import os, time
>
> try:
> menu=open('C:/Python22/Menu.txt','r')
> outf=open('C:/Python22/Menu.txt','w')
I think this one should be "Menu.tmp" - otherwise you'll
stamp all over the file you are reading - not a good idea!
> os.rename('C:/Python22/Menu.txt', 'C:/Python22/Menu.bak')
> os.rename('C:/Python22/Menu.tmp', 'C:/Python22/Menu.txt')
And this line only makes sense if your new ffile is Menu.tmp...
> except:
> print 'ERROR: Failed to create new menu'
Hmm, The except could mean several things aside from that
but I guess it illustrates the idea OK. I can't see anything
that would cause a syntax error...
Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld