I'm just starting with Python, one of my first scripts is to be a very simple work log. I run it, when i finish work I press enter and it puts worktime into html file that I share with guy who hire me.
Here is the code, please do not facepalm too much, these are my first steps:
#!/usr/bin/python
import datetime
start = datetime.datetime.now()
startt = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print "Counting worktime started."
confirm = raw_input("Press enter when work finished ")
finish = datetime.datetime.now()
delta = finish - start
print datetime.datetime.now()
print delta.seconds
work_time=str(datetime.timedelta(seconds=delta.seconds))
finisht=str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
note = raw_input("What were you doing? ")
line1=['<span>','Work started: <b>', startt, '</b></span><br />']
line1f=' '.join(line1)
line2=['<span>','Work ended: <b>', finisht, '</b></span><br />']
line2f=' '.join(line2)
line3=['<span>','Work duration: <b>', work_time,'</b></span><br />']
line3f=' '.join(line3)
line4=['<span>','Note: <b>', note,'</b></span><br /><br />']
line4f=' '.join(line4)
with open("/srv/www/worklog.html","a") as worklog:
worklog.write(line1f)
worklog.write("\n")
worklog.write(line2f)
worklog.write("\n")
worklog.write(line3f)
worklog.write("\n")
worklog.write(line4f)
worklog.write("\n")
worklog.write("<span> ========================= </span><br /><br />")
worklog.write("\n")
and here is the worklog.html file:
<html>
<head></head>
<body style="background-color: #195B83;color:#FFF;margin: 0px;">
<style>
span {font-size: 12px;margin-left: 5px;}
.logo {float: right;margin-right: 10px;margin-top:5px;}
.first-div {background-color: #FFF; color:#195B83;width:100%;}
.sec-div {margin-left: 5px;}
</style>
<div class="first-div">
<img src="logo.png" class="logo" />
<div class="sec-div">
<h1>simple worklog 1.0</h2>
</div>
</div>
<br />
<span> ========================= </span><br /><br />
<span> Work started: <b> 2014-09-11 13:40:26 </b></span> <br />
<span> Work ended: <b> 2014-09-11 13:40:29 </b></span> <br />
<span> Work duration: <b> 0:00:02 </b></span> <br />
<span> Note: <b> Testing </b></span><br /><br />
<span> ========================= </span><br /><br />
And it works!
My question is - how can I include
</body></html>
tags? I tried with .replace, but my experiments failed with whole file purged. Can you give me a hint on how to make this script to keep them at the end of worklog.html ?
EDIT:
Thanks to awesome hints below, I have rewritten the code and I think that now it has a lot more sense, you can find it here:
main_script (add log to csv and add data to the website): http://pastebin.com/ZbCqJ9p9
page_refresher (no worklog adding, just put the data on website): http://pastebin.com/3hi077RK
template (with bootstrap css): http://pastebin.com/xZ7VmE1U
data files format: http://pastebin.com/0KNAXuqh
and it looks like that: http://elysium.c-call.eu/sworklog/
It's surely not the highest class and has some issues, but it's a lot better than the piece of crap I came here with :)
Thank you very much.