Michael Jones
Michael Jones

Reputation: 35

Python Nested Dictionarys

I have this file system that I am trying to get to work. My problem so far is when printing out all of the files. I can get the name to print out, but then I do not know how to access the date and text.

My Full Code

import datetime
import time

files = {}
# g = open('files.txt', 'r')
# g.read(str(files))
# g.close()

def startup():
    print "\n          -------------------          "
    print "          FILE SYSTEM MANAGER          "
    print "          -------------------          "
    print "\n What would you like to do with your files?"
    print "   To make a new file type in: NEW"
    print "   To edit a current file type in: EDIT"
    print "   To view all current files type in: ALL"
    print "   To search a specific file type in: SEARCH"
    chooser = raw_input("\n Please enter NEW, EDIT, ALL, or SEARCH: ")
    if chooser.lower() == "new":
        newfile()
    elif chooser.lower() == "edit":
        editfiles()
    elif chooser.lower() == "all":
        allfiles()
    elif chooser.lower() == "search":
        searchfiles()
    else:
        startup()

#-- New File -------------------------------
def newfile():
    filename = ""
    filetext = ""
    while filename == "":
        print "--------------------------------------------"
        filename = raw_input("\n Please input your new files name: ")
    while filetext == "":
        filetext = raw_input("\n Please input the text for your new file: ")
    filedate = datetime.date.today()
    files[filename] = {filedate:filetext}
    # f = open ('files.txt', 'w')
    # f.write(str(files))
    # f.close()
    print "\n File Added"
    print "\n--------------------------------------------"
    print "\n          -------------------          "
    print "          FILE SYSTEM MANAGER          "
    print "          -------------------          "
    print "\n What would you like to do with your files?"
    print "   To make a new file type in: NEW"
    print "   To edit a current file type in: EDIT"
    print "   To view all current files type in: ALL"
    print "   To search a specific file type in: SEARCH"
    chooser = raw_input("\n Please enter NEW, EDIT, ALL, or SEARCH: ")
    if chooser.lower() == "new":
        newfile()
    elif chooser.lower() == "edit":
        editfiles()
    elif chooser.lower() == "all":
        allfiles()
    elif chooser.lower() == "search":
        searchfiles()
    else:
        startup()

def editfiles():
    pass
def allfiles():
    for i in files:
        print "--------------------------------------------"
        print "File Name: " + str((i))
        for i[filedate] in files:
            print "File Date: " + (i[filedate])
def searchfiles():
    pass

startup()

It works correctly and prints the name of each file with this:

for i in files:
    print "--------------------------------------------"
    print "File Name: " + str((i))

then after that I can't seem to access the date and text. I am saving the dictionaries to the dictionary file like this:

files[filename] = {filedate:filetext}

The code I am using to try to get the filedate is this:

for i in files:
            print "--------------------------------------------"
            print "File Name: " + str((i))
            for i[filedate] in files:
                print "File Date: " + (i[filedate])

and the error it gives me is >> NameError: global name 'filedate' is not defines

EDIT how would I also add the filetext to the for loop for it to print? THANK YOU

Upvotes: 0

Views: 75

Answers (3)

metatoaster
metatoaster

Reputation: 18898

First off, you are iterating through the dictionary, and by default only the keys are returned, so when you do

    for i in files:

Only the keys (names of the files) are stored in i, so i[filedate] would return nothing even if filedate was defined. You need to use dict.items() for both cases, which return both the key and value as pairs. Correcting your code, it will become this:

def allfiles():
    for filename, filevalue in files.items():
        print "--------------------------------------------"
        print "File Name: " + filename
        for filedate, filetext in filesvalue.items():
            print "File Date: " + filedate

Upvotes: 3

Joran Beasley
Joran Beasley

Reputation: 113940

for a_date in files[i]:
    print "File Date: " + a_date

I think would work fine ...

it becomes much more clear if you change your variable names

def allfiles():
    for fileName in files:
        print "--------------------------------------------"
        print "File Name: " + fileName
        for a_date in files[fileName]:
            print "File Date: " + a_date

Upvotes: 2

Chachmu
Chachmu

Reputation: 8182

filedate is only defined in the function newfile(). If you want to be able to use it in the function allfiles() then you either need to re-declare it there or make the variable global.

Upvotes: 0

Related Questions