datetime - Python not writing to file -


i'm making script reads through .txt file , proceeds use information, , date, create dynamic html files. worked yesterday, today, seems doesn't want write file...

here source code.

from __future__ import print_function import httplib2 import os import io import shutil import datetime import random import re apiclient.http import mediaiobaseupload, mediaiobasedownload datetime import date apiclient import discovery import oauth2client oauth2client import client oauth2client import tools  def filerino():      daginfo     = str(datetime.datetime.today().weekday())     ukenr       = (datetime.datetime.today().isocalendar()[1])     dato        = datetime.date.today()     day         = datetime.date.today().day     month       = datetime.date.today().month     yesterday   = (day - 1)     tomorrow    = (day +1)     todayerino  = str(day) + "/" + str(month)     yesterdayerino = str(yesterday) + "/" + str(month)     tomorrowerino = str(tomorrow) + "/" + str(month)     currentweek = (datetime.datetime.today().isocalendar()[1])     lastweek    = ((datetime.datetime.today().isocalendar()[1]) - 1)     currentweekerino = "uke " + str(currentweek)     lastweekerino = "uke " + str(lastweek)        file=open("plan.txt", "r")     if daginfo == "3":         daginfo = ("matte\n" + "naturfag\n" + "engelsk\n" + "tof\n")         dag = ("torsdag")         imorgen = ("fredag")     elif daginfo == "0":         daginfo = ("samfunnsfag\n" + "geografi\n" + "norsk / engelsk\n")         dag = ("mandag")         imorgen = ("torsdag")     elif daginfo == "1":         daginfo = ("naturfag\n" + "spraak\n" + "engelsk\n" + "matte\n")         dag = ("tirsdag")         imorgen = ("onsdag")     elif daginfo == "2" , ukenr %2 == 0:         daginfo = ("gym\n" + "matte\n" + "norsk\n")         dag = ("onsdag")         imorgen = ("torsdag")     elif daginfo == "2" , ukenr %2 != 0:         daginfo = ("gym\n" + "matte\n")         dag = ("onsdag")         imorgen = ("torsdag")     elif daginfo == "4":         daginfo = ("spraak\n" + "norsk\n" + "engelsk\n" + "tof\n")         dag = ("fredag")         imorgen = ("laurdag")     elif daginfo == "5":         daginfo = ("ingen fag idag, siden det er helg! :d")         dag = ("laurdag")         imorgen = ("saundag")     elif daginfo == "6":         daginfo = ("ingen fag idag, siden det er helg! :d")         dag = ("saundag")         imorgen = ("mandag")       file=open("plan.txt","r")     s = file.read()     dayresult = ((s.split(str(tomorrowerino)))[1].split(str(todayerino))[0]).strip()     dayresult = dayresult[:dayresult.rfind('\n')]     lines = re.split("\n", dayresult)     timeserino = dayresult.count("\n")     try:          fagtitle = {}         x in range(-1, timeserino):             x += 1             fagtitle[x] = lines[x]          fagcontent = {}         x in range(-1, timeserino):             x += 1             fagcontent[x-1] = lines[x]          if timeserino == 1: #if there 1 lines within 2 dates.             file=open("/testfolder/index.html", "w")             file.write("""            <!doctype html>            <html lang="en">            <head>               <meta charset="utf-8">               <title>instaplan</title>               <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mtjoasx8j1au+a5wdvnpi2lkffwweaa8hdddjzlplegxhjvme1fgjwpgmkzs7" crossorigin="anonymous">              <link rel="stylesheet" href="main.css">            </head>             <body>                <div class="jumbotron" id="nbn">                  <h1 id="sub">""" + (dag) + """ """ + str(dato.day) + """/""" + str(dato.month) + """ """ + """uke""" + """ """ + str(ukenr) + """:</h1>                  <h1 id="mid"><small>""" + (daginfo) + """</small></h1>                </div>                <div class="jumbotron" id="nbn">                  <h1 id="mid"><small>lekser til """ + (imorgen) + """:</small></h1>                </div>                <div class="jumbotron" id="nbm">                  <h1 id="sub">""" + fagtitle[0] + """</h1>                  <h1><small>""" + fagcontent[0] + """</small></h1>                </div>            </body>            </html>            """)         elif timeserino == 3: #if there 3 lines within 2 dates.             file=open("/testfolder/index.html", "w")             file.write("""            <!doctype html>            <html lang="en">            <head>               <meta charset="utf-8">               <title>instaplan</title>               <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mtjoasx8j1au+a5wdvnpi2lkffwweaa8hdddjzlplegxhjvme1fgjwpgmkzs7" crossorigin="anonymous">              <link rel="stylesheet" href="main.css">            </head>             <body>                <div class="jumbotron" id="nbn">                  <h1 id="sub">""" + (dag) + """ """ + str(dato.day) + """/""" + str(dato.month) + """ """ + """uke""" + """ """ + str(ukenr) + """:</h1>                  <h1 id="mid"><small>""" + (daginfo) + """</small></h1>                </div>                <div class="jumbotron" id="nbn">                  <h1 id="mid"><small>lekser til """ + (imorgen) + """:</small></h1>                </div>                <div class="jumbotron" id="nbm">                  <h1 id="sub">""" + fagtitle[0] + """</h1>                  <h1><small>""" + fagcontent[0] + """</small></h1>                </div>                <div class="jumbotron" id="nbm">                  <h1 id="sub">""" + fagtitle[2] + """</h1>                  <h1><small>""" + fagcontent[2] + """</small></h1>                </div>            </body>            </html>            """)         elif timeserino == 5: #if there 5 lines within 2 dates.             file=open("/testfolder/index.html", "w")             file.write("""            <!doctype html>            <html lang="en">            <head>               <meta charset="utf-8">               <title>instaplan</title>               <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mtjoasx8j1au+a5wdvnpi2lkffwweaa8hdddjzlplegxhjvme1fgjwpgmkzs7" crossorigin="anonymous">              <link rel="stylesheet" href="main.css">            </head>             <body>                <div class="jumbotron" id="nbn">                  <h1 id="sub">""" + (dag) + """ """ + str(dato.day) + """/""" + str(dato.month) + """ """ + """uke""" + """ """ + str(ukenr) + """:</h1>                  <h1 id="mid"><small>""" + (daginfo) + """</small></h1>                </div>                <div class="jumbotron" id="nbn">                  <h1 id="mid"><small>lekser til """ + (imorgen) + """:</small></h1>                <div class="jumbotron" id="nbm">                  <h1 id="sub">""" + fagtitle[0] + """</h1>                  <h1><small>""" + fagcontent[0] + """</small></h1>                </div>                <div class="jumbotron" id="nbm">                  <h1 id="sub">""" + fagtitle[2] + """</h1>                  <h1><small>""" + fagcontent[2] + """</small></h1>                </div>                <div class="jumbotron" id="nbm">                  <h1 id="sub">""" + fagtitle[4] + """</h1>                  <h1><small>""" + fagcontent[4] + """</small></h1>                </div>            </body>            </html>            """)          elif timeserino == 7: #if there 7 lines within 2 dates.             file=open("/testfolder/index.html", "w")             file.write("""            <!doctype html>            <html lang="en">            <head>               <meta charset="utf-8">               <title>instaplan</title>               <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mtjoasx8j1au+a5wdvnpi2lkffwweaa8hdddjzlplegxhjvme1fgjwpgmkzs7" crossorigin="anonymous">              <link rel="stylesheet" href="main.css">            </head>             <body>                <div class="jumbotron" id="nbn">                  <h1 id="sub">""" + (dag) + """ """ + str(dato.day) + """/""" + str(dato.month) + """ """ + """uke""" + """ """ + str(ukenr) + """:</h1>                  <h1 id="mid"><small>""" + (daginfo) + """</small></h1>                </div>                <div class="jumbotron" id="nbn">                  <h1 id="mid"><small>lekser til """ + (imorgen) + """:</small></h1>                <div class="jumbotron" id="nbm">                  <h1 id="sub">""" + fagtitle[0] + """</h1>                  <h1><small>""" + fagcontent[0] + """</small></h1>                </div>                <div class="jumbotron" id="nbm">                  <h1 id="sub">""" + fagtitle[2] + """</h1>                  <h1><small>""" + fagcontent[2] + """</small></h1>                </div>                <div class="jumbotron" id="nbm">                  <h1 id="sub">""" + fagtitle[4] + """</h1>                  <h1><small>""" + fagcontent[4] + """</small></h1>                </div>                <div class="jumbotron" id="nbm">                  <h1 id="sub">""" + fagtitle[6] + """</h1>                  <h1><small>""" + fagcontent[6] + """</small></h1>                </div>            </body>            </html>            """)     except indexerror: #if detects indexerror because there either aren't lines between dates         file=open("/testfolder/index.html", "w")         file.write("""        <!doctype html>        <html lang="en">        <head>           <meta charset="utf-8">           <title>instaplan</title>           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mtjoasx8j1au+a5wdvnpi2lkffwweaa8hdddjzlplegxhjvme1fgjwpgmkzs7" crossorigin="anonymous">          <link rel="stylesheet" href="main.css">        </head>         <body>            <div class="jumbotron" id="nbn">              <h1 id="sub">""" + (dag) + """ """ + str(dato.day) + """/""" + str(dato.month) + """ """ + """uke""" + """ """ + str(ukenr) + """:</h1>              <h1 id="mid"><small>""" + (daginfo) + """</small></h1>            </div>            <div class="jumbotron" id="nbn">              <h1 id="mid"><small>lekser til """ + (imorgen) + """:</small></h1>            </div>            <div class="jumbotron" id="nbm">              <h1 id="sub">ayylmao</h1>            </div>        </body>        </html>        """) if __name__ == '__main__':     main()     filerino() 

it writes file called index.html. think problem lies in try statement, tried remove , didn't affect in way.

thank you.

1) make sure meets of scenarios. didn't have else condition below, timeserino may not meet of 1-7 scenarios

if timeserino == 1:    # write elif timeserino ==2:    # write ... elif timeserino == 7:    # write else:    write 

2) close file when finish writing


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -