1

Basically, what I want to know is, is there a simpler way to write this that does not require the import os module? I want to create a simple webpage from Python using an HTML file. And while this does exactly what I want, I cannot seem to come up with a simple/basic way, or a way that does not involve the import os module.

import os

name = input("Enter your name here: ")
persona = input("Write a sentence or two describing yourself: ")

with open('mypage.html', 'rt') as file:
    with open('temp_mypage.html', 'wt') as new:
        for line in file:
            line = line.replace('some_name', name)
            line = line.replace('some_persona', persona)
            new.write(line)

os.remove('mypage.html')
os.rename('temp_mypage.html', 'mypage.html')

HTML code:

<html>
<head>
<body>
<center>
<h1>
some_name
# input into the file
</h1>
</center>
<hr />
some_persona
<hr />
</body>
</html>
7
  • Use Flask: flask.palletsprojects.com/en/1.1.x Commented Nov 19, 2020 at 2:02
  • Secondly, why would you want to do that Commented Nov 19, 2020 at 2:03
  • Because I did not realize that my instructor did not want us to use import os and now I am trying to figure out how to rewrite it. Commented Nov 19, 2020 at 2:04
  • Oh so its homework Commented Nov 19, 2020 at 2:05
  • I have something to do with that Commented Nov 19, 2020 at 2:05

1 Answer 1

1

You don't need os module and temporary file, instead you can just add to a variable and write back

name = input("Enter your name here: ")
persona = input("Write a sentence or two describing yourself: ")
new = ""

with open('mypage.html', 'rt') as file:
        for line in file:
            line = line.replace('some_name', name)
            line = line.replace('some_persona', persona)
            new += line.strip()

with open('mypage.html','w') as f:
        f.write(new)

And for tasks like this it is not the recommended way. Use a web framework like Flask (Django will be top heavy for this simple one), CGI scripts are now outdated

Sign up to request clarification or add additional context in comments.

7 Comments

Oh... thats what he wanted. Well, I thought he was to activate the website to a web page or something. I am a dumb boi
Ah, I see, this works really well! Before I uploaded the working code, I was trying to open it in writer mode with the "for line" statement, but it kept telling me it was not a readable object and that threw me off. For the most part this makes sense, and it does work, can you possibly explain the ' new = "" ' line to me please?
new="" creates a new empty string variable and also to both read/write from a file open in rw mode, please upvote (click ^ looking button) and accept (click big check mark), welcome :-)
Oh okay, alright now I understand it a lot better. This was an amazing fix! Thanks man! I appreciate it
@WasifHasan I did not to think to ask before, and I hope it is not a bother, but is it possible to loop this program? And what I mean by that is, how can I run the program over and over without ceating a new html file every time, because as a test run, I entered JK as the input in both fields and now my html file is permanently set to JK. If that makes sense. I am unsure of how to google this question..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.