Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does it exit?
#1
Star 
I was making an app, that takes input and writes it in a csv file.
But when I run, it just exits.
Script,
from tkinter import*
import time
import csv
import os.path
import sys

if not os.path.exists("timetable.csv"):
   #run start up script
   print("Cache was Deleted!\nCreating new Cache")
   time.sleep(3)
   with open ("timetable.csv", "w") as timetable :
       fieldnames = ["From time", "To Time", "Thing To Do"]
       csv_writer = csv.DictWriter(timetable, fieldnames = fieldnames, delimiter = ",")
       csv_writer.writeheader()

       how_many_things = input("How many things are there?\n")

       while how_many_things > 1:
           from_time = input(how_many_things + "Please enter from time.\n")
           to_time = input(how_many_things + "Please enter to time.\n")
           thing_done = input(how_many_things + "Please enter thing to do.\n")
           how_many_things -= 1
           csv_writer.writerow([from_time, to_time, thing_done])
print("Cache Made!")
input()
Any ways to solve this problem?
Reply
#2
I don't believe that it 'just exits'.

Some observations:

- tkinter and sys imported but not used.
- it's not recommended to use * import in files (it's ok in interactive interpreter)
- if file happens to exist main part of code (under if) will not run
- even if file exists two last rows are executed (print and input). NB! If you run file repeatedly without deleting csv file created between runs it will not run if part as file already exists
- you can't compare integer and string (line # 18), it will raise error
- instead of os.path.exists there is more canonical way of doing it (using try, and file mode 'x'):

try:
    with open('timetable.csv', 'xt') as timetable:
        # do stuff
except FileExistsError:
    # do other stuff
sridhar likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,823 Nov-12-2022, 01:37 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020