Can someone fix my code?

Joshua Macy amused at webamused.com
Mon Jan 31 03:01:45 EST 2000


It would be easier to help you if you included the actual error that
you're getting, but it appears to me that part of your problem is that
you're forgetting which variables hold what (one of the reasons that
it's generally not a good idea to have too many variables named
temp_this and temp_that).  Also, commenting your intent can help.
 The specific problem seems to be that you attempt to use your output
file as input.  body is a file opened for writing, so attempting to loop
over it as in:
	for line in body:
		body.write(line)
generates an "TypeError: loop over non-sequence" exception.  That makes
sense: body is a file, not a sequence.  If what you're trying to do is
combine two existing files into a new file, you're going to need another
file.  Something like:
    body_file = open(body_path, "r")  # open the text of the web page
body FOR READING
    body = body_file.readlines()      # now body is a sequence, so for
line in body: should be okay
    out_path = raw_input("Please type the path of the output file: ")
    out_file = open(out_path, "w")  # open the output file
Then change body.write(line) to out_file.write(line) in the rest of the
file.

BTW, if you don't mind some suggestions on coding style, it's generally
regarded as poor practice to mix the interface (in this case the
questions about the file names) up with the code that's really doing the
work.  If, for instance, you moved the questions up into the ssi_or_exit
function, and then passed the file names in to the ssi function, it
would be a lot easier down the road to reuse the ssi function, e.g. to
provide a batch mode that gets the file names from the command line, or
from another file.

I hope this helps.

  Joshua



bovinesoft at my-deja.com wrote:
> 
> 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.
> 
> 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()
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.



More information about the Python-list mailing list