Can someone fix my code?
Phil Mayes
nospam at bitbucket.com
Mon Jan 31 02:45:39 EST 2000
bovinesoft at my-deja.com wrote in message <8733rg$ral$1 at nnrp1.deja.com>...
>I am a complete Python newbie and am trying to learn it as my first real
>programming language (I have dabbled in C, Java, and Assembly, but never
>really used them). I am trying to write a program to get some practice,
>but am getting errors when I try to run it. If someone could take a
>look at my code and tell me what is wrong with it, I would be greatly
>appreciative.
First: post the error message in future :)
Second: I presume you are merging two files into a third.
You're halfway there, but don't open the second input file! Instead,
you're treating the output file as the second input:
> for line in body:
> body.write(line)
and also iterating over the handle; you need to read the lines, like
temp2.
Third (and getting pickier), I don't like the multiple calls to
ssi_or_exit. Do this instead:
def ssi_or_exit():
while 1:
sorx = raw_input("Would you like to generate HTML <g> or exit <x>? ")
if sorx == "g": # if they want to generate a web page
ssi()
elif sorx == "x": # if they want to exit
sys.exit("Thank you for using Bovine No Server SSI!")
>import sys # to use the exit() function
>
>def ssi_or_exit():
> sorx = raw_input("Would you like to generate HTML <g> or exit <x>? ")
> if sorx == "g": # if they want to generate a web page
> ssi()
> elif sorx == "x": # if they want to exit
> sys.exit("Thank you for using Bovine No Server SSI!")
> else:
> ssi_or_exit() # try it again
>
>def ssi():
> body_path = raw_input("Please type the path of the body section: ")
> body = open(body_path, "w") # open the text of the web page body
> temp_path = raw_input("Please type the path of the HTML template: ")
> temp = open(temp_path, "r") # open the HTML template
> temp2 = temp.readlines()
> where_temp = raw_input("Put the template before <b> or after <a> the
>body? ")
> if where_temp == "b": #around here is where my trouble starts
> for line in temp2:
> body.write(line)
> for line in body:
> body.write(line)
> else:
> for line in body:
> body.write(line)
> for line in temp2:
> body.write(line)
> body.close()
> temp.close()
> ssi_or_exit()
>
>ssi_or_exit()
--
Phil Mayes pmayes AT olivebr DOT com
More information about the Python-list
mailing list