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 Using Databases in Python Gettin' CRUD-y With It Add An Entry

Oti Oritsejafor
Oti Oritsejafor
3,281 Points

when I try to run mine, it says "No such file or directory"

I named mine journal.py, When I enter "./journal.py" outside the python shell it tells me "No such file or directory."

Ryan Oakes
Ryan Oakes
9,787 Points

What exactly are you trying to do?

Are you in the command line? Hit "ls" to see if journal.py is in your current directory.

Also, if you're trying to mess around in the Python shell and utilize assets from your journal.py, you'll need to import them into the shell.

7 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

No, I mean... like, for me, it'd be:

$ python journal.py

And then something would get outputted to the screen. What are you running and what's the error message that's coming back?

Oti Oritsejafor
Oti Oritsejafor
3,281 Points

I try to run

./journal.py

And it gives me

: No such file or directory
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

OK, what if you do python3 journal.py?

Are you sure you didn't misspell anything? Also, did you delete the file (you can type LS in lower case letters to see the files)?

I hope you will figure it out because it works just fine for me :smile:

Oti Oritsejafor
Oti Oritsejafor
3,281 Points

Thanks, but when I typed ls in lower case all the files seem to be intact.

Maybe you didn't add permissions yet. Try typing this into the command line (don't type the dollar sign, juts type everything in front of it):

$ chmod +x journal.py

Hope it helps :smile:

Happy coding!

Oti Oritsejafor
Oti Oritsejafor
3,281 Points

Ryan Oakes , when I type 'ls" it shows:

journal.db journal.py students.db students.py

Ryan Oakes
Ryan Oakes
9,787 Points

I see. Maybe we can help you better if you show us your code?

Also, if you're not using workspaces and trying to run the program, try "python3 journal.py" in the command line and see if that works.

Oti Oritsejafor
Oti Oritsejafor
3,281 Points

Ryan Oakes here is my code for journal.py(I am running it in workspaces:

#!/usr/bin/env python3
import datetime

from collections import OrderedDict
from peewee import *
import sys


db = SqliteDatabase('journal.db')


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

    class Meta:
        database = db

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


def menu_loop():
    """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 finsihed.")
    data = sys.stdin.read().strip()

    if data:
        if input("Save entry? [Y/N]").lower() != 'n':
            Entry.create(content=data)
            print("Saved successfully.")

def view_entry():
    """View previous entries"""


def delete_entry():
    """Delete previous entry"""


menu = OrderedDict([
        ('a', add_entry),
        ('b', view_entry),

    ])     


if __name__ == '__main__':
    initialize()
    menu_loop()
Oti Oritsejafor
Oti Oritsejafor
3,281 Points

Kenneth Love help please?(Also I changed the line with: print("{}) {}".format(key, value_.doc)) to print("{}) {}".format(key, value.doc_)) but that didn't help

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Can you copy and paste the entirety of what you're running and the error message? That'll help with debugging.

Oti Oritsejafor
Oti Oritsejafor
3,281 Points

Okay thanks, here it is( I named it journal.py rather than diary.py): '''python

!/usr/bin/env python3

from collections import OrderedDict import datetime import sys

from peewee import *

db = SqliteDatabase('journal.db')

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

class Meta:
    database = db

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

def menu_loop(): """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 you entry. Press ctrl+d when finished.") data = sys.stdin.read().strip()

if data:
    if input('Save entry? [Yn] ').lower() != 'n':
        Entry.create(content=data)
        print("Saved successfully!")

def view_entries(search_query=None): """View previous entries.""" entries = Entry.select().order_by(Entry.timestamp.desc()) if search.query: entries = entries.where(Entry.content.contains(search_query))

for entry in entries:
    timestamp = entry.timestamp.strftime('%A %B %d, %Y %I:%M%p)
    print(timestamp)
    print(=*len(timestamp))
    print(entry.content)                                     
    print('n) for next entry')
    print('q) to quit')    

    next_action = input('Action:  ').lower().strip()

    if next_action == 'q':
        break

def search_entries(): """search entries for a string.""" view_entries(input(Search query: ))

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

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

if name == 'main': initialize() menu_loop()

'''

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Well that's what I'm trying to figure out :)

OK, so what about /usr/bin/env python3 journal.py? Does that work?

Oti Oritsejafor
Oti Oritsejafor
3,281 Points

Kenneth Love When I try "python3 journal.py", it works! Thanks a lot and can I know why it only works with that?