Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Denny Ho
Denny Ho
4,472 Points

EOFError for Python database: add an entry

can someone please help me understand why im getting this error?

C:\Python34\python.exe C:/Users/Denny/PycharmProjects/treehousepython/diary.py

Enter 'q' to quit.

a) Add an entry.

v) View previous entries.

Action: a

Enter you entry. Press ctrl+d when finished.

adfasdfsdf

Traceback (most recent call last):

File "C:/Users/Denny/PycharmProjects/treehousepython/diary.py", line 66, in <module>

Save entry? [Yn] menu_loop()

File "C:/Users/Denny/PycharmProjects/treehousepython/diary.py", line 36, in menu_loop menuchoice

File "C:/Users/Denny/PycharmProjects/treehousepython/diary.py", line 45, in add_entry if input('Save entry? [Yn] ').lower() != 'n':

EOFError: EOF when reading a line

Process finished with exit code 1

running windows 8.1, using pycharms, connecting to mysql. everything works up to when i press ctrl+d to end but the script errors and doesn't go on to save entry.

from collections import OrderedDict
import datetime
import sys

from playhouse.db_url import connect
from peewee import *

mysql_db = connect('mysql://python_user:***@localhost:3306/python_work')
# mysql_db = MySQLDatabase('diary.db')  , host='localhost', user='python_user', passwd='conrang


class Entry(Model):
    content = TextField()
    timestamp = DateTimeField(default=datetime.datetime.now)

    class Meta:
        database = mysql_db


def initialize():
    """Create the database and the table if they don't exist."""
    mysql_db.connect()
    mysql_db.create_tables([Entry], safe=True)


def menu_loops():
    """Show the menu"""
    choice = None

    while choice != 'q':
        print("Enter 'q' to quit.")
        for key, value in menu.items():
            print('{}) {}'.format(key, value.__doc__))
        choice = input('Action: ').lower().strip()

        if choice in menu:
            menu[choice]()


def add_entry():
    """Add an entry."""
    print("Enter your entry. Press ctrl+d when finished.")
    data = sys.stdin.read().strip()

    if data:
        try:
            savein = input('Save entry? [Yn] ').lower()
            if savein != 'n':
                Entry.create(content=data)
                print("Saved successfully")
        except EOFError:
            print("what goin on here?")




def view_entries():
    """View previous entries."""


def delete_entry(entry):
    """Delete an entry."""

menu = OrderedDict([
    ('a', add_entry),
    ('v', view_entries),
])

if __name__ == '__main__':
    initialize()
    menu_loops()

2 Answers

Denny Ho
Denny Ho
4,472 Points

hey kenneth, thanks for responding.

For anyone who might have the same problem/is interested, here are some things i've run into since posting.

so after playing around with it yesterday, i found that i needed to hit enter first, then press ctrl+d. If i didn't have a new line, apparently it didn't capture anything i typed. in any case, it still did not solve the EOFError, only helps to capture the stdin.

I added some try/except statements in the while loop before the input('Action: ') part. from there, running my script caused an infinite loop as it just kept getting EOFErrors, i guess.

i was basically at my wits end until i decided to just try running my script on powershell, outside of my IDE. IT WORKED. just enter, ctrl+z, enter after typing my entry and the script works as expected. idk why that windows way of doing an EOF doesn't work on pycharm. on pycharm, only ctrl+d does anything to make the script move forward, yet i suspect it is also the culprit that makes it have an EOFError in the first place. maybe the python console on pycharm was meant to run on a linux environment and my windows os is just messing it up. sigh, guess i should start figuring out how to use cygwin.

nothing conclusive but hope my headache saves someone in the future from one.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Everything looks fine so my first guess is "maybe it's not ctrl-d on Windows to end the stdin?" I didn't find anything conclusive but several things said it was ctrl-z for Windows. Give that a try?

James N
James N
17,864 Points

nope! that doesn't work! I REALLY like this program and want it to work. could i code something in to end the stdin? like pushing esc or f11? thanks!

James N
James N
17,864 Points

never mind! that was wierd. it wasn't working before (CTRL+Z to end the stdin) but now it does! LOL thanks anyway.