Cody
Cody

Reputation: 15

Writing python to a file. error: attribute?

for school project I have to provide the following: Generate a Personal Web Page: Write a program that asks the user (you) for the following information:

Enter his/her name Enter degree major Enter your future career and a brief description of the career Once the user has entered the requested input, the program should create an HTML file, write the html file utilizing the tags below, and display the user entered text in the placeholder of the red text for a simple Web page.

the code below is what I have so far according to all the extras my teacher is asking for (she/we are aware majority is unnecessary for it to run)… once I run the following code I receive the error (posted at the end), I have tried several different variations according to the book I am using (starting out with python 4th edt - gaddis) but unfortunately I haven't been able to come up with a fix... please advise!

def main():
    # Accept name from the user
    name = input("Enter your name: ")

    # Accept degree/major from the user
    major = input("Enter your degree/major: ")

    # Accept describe yourself from the user.
    print("Enter your future career, a brief description of the career below")
    describe = input("Describe yourself: ")

    # Create a file object
    file = open(r'C:\Users\BEASTMODE\Desktop\person.html', "w")
    file.write_html
    file.write_head
    file.write_body
    file.close()
    return name, major, describe, file


def write_html(file):
    file.write("<html>")
    write_head()
    write_body()
    file.write("</html>")


def write_head(file):
    file.write("<head>")
    file.write("<title>test page</title>")
    file.write("</head>")


def write_body(file, name, major, describe):
    file.write("<body>")
    file.write("<center>")
    file.write("<h1>")
    file.write(name)
    file.write("</h1>")
    file.write("<hr />")
    file.write("<h2>")
    file.write(major)
    file.write("</h2>")
    file.write("<hr />")
    file.write(describe)
    file.write("</center>")
    file.write("<hr />")
    file.write("</body>")


main()

----- ERROR: ERROR: ------

AttributeError                            Traceback (most recent call last)
c:\Users\BEASTMODE\Desktop\HobbsC_Webpage.py in 
     49 
     50 
---> 51 main()

c:\Users\BEASTMODE\Desktop\HobbsC_Webpage.py in main()
     12     # Create a file object
     13     file = open(r'C:\Users\BEASTMODE\Desktop\person.html', "w")
---> 14     file.write_html
     15     file.write_head
     16     file.write_body

AttributeError: '_io.TextIOWrapper' object has no attribute 'write_html'

Upvotes: 0

Views: 79

Answers (1)

chepner
chepner

Reputation: 531185

Your functions are, well, functions, not methods (which you aren't calling anyway).

# Create a file object
file = open(r'C:\Users\BEASTMODE\Desktop\person.html', "w")
write_html(file)
# write_head(file)
# write_body(file)
file.close()

Note that write_html already calls write_head and write_body; you don't need to call them explicitly and again. (The calls do need to be fixed inside write_html, though.)

Upvotes: 2

Related Questions