使用文本菜单从另一个函数调用函数

时间:2016-01-25 15:13:56

标签: python python-3.x

我有两个函数,一个是我的main_menu,它允许用户输入一个数字来调用另一个函数。当选择数字1时,我得到错误“NameError:name'createDirectory'未定义”。我只是觉得它就像调用createDirectory函数一样简单,但显然不是。有人能指出我正确的方向吗?

from os import makedirs, path
# import shutil


def main_menu():
    print("PLEASE CHOOSE AN OPTION BELOW BY ENTERING THE CORRESPONDING NUMBER:  \n")

    print("[1]: CREATE CASE FOLDER STRUCTURE")

    print("[2]: BACK CASE UP TO SERVER")

    print("[3]: CLOSE PROGRAMME \n")


main_menu()

while True:
    # choice = int(input("ENTER YOUR CHOICE HERE:"))
    try:
        choice = int(input("ENTER YOUR CHOICE HERE:"))
        if choice == 1:
            createDirectory(targetPath)
            main_menu()
            break

        elif choice == 2:
            # backUpCase('src', 'dst')
            main_menu()
            break

        elif choice == 3:
            break

        else:
            print("INVALID CHOICE!! PLEASE ENTER A NUMBER BETWEEN 1-3")
            main_menu()

    except ValueError:
        print("INVALID CHOICE!! PLEASE ENTER A NUMBER BETWEEN 1-3")

    exit()

main_menu()


def createDirectory(targetPath):
    if not path.exists(targetPath):
        makedirs(targetPath)
        print('Created folder ' + targetPath)
    else:
        print('Path ' + targetPath + ' already exists, skipping...')

    # MAIN #

    print('''Case Folder Initialiser
    v 1.1 2015/09/14
    A simple Python script for creating the folder structure required for        new cases as follows;

    05 DF 1234 15
    +--Acquisitions
    ¦  ---QQ1
    ¦  ---QQ2
    ¦  ---...
    +--Case File
    ¦  ---X Ways
    ¦  +--EnCase
    ¦  ¦  +--Temp
    ¦  ¦  +--Index
    ¦  +--NetClean
    +--Export
       ---X Ways Carving

    All user inputs are not case sensitive.
    ''')

    driveLetter = input('Enter the drive letter for your WORKING COPY disc:          ').upper()

    limaReference = input('Enter the Lima reference number, such as 05 DF 12345 15: ').upper()

    rootPath = driveLetter + ':/' + limaReference + '/'

    print('You will now enter your exhibit references, such as QQ1. Press enter at an empty prompt to stop adding further exhibits.')

    exhibits = []
    while True:
        exhibit = input('Enter exhibit reference: ').upper()
        if not exhibit:
            print('You have finished adding exhibits and the folders will  now be created.')
            break
        exhibits.append(exhibit)

    for exhibit in exhibits:
        # targetPath = rootPath + 'Acquisitions/' + exhibit + '/'
        # targetPath2 = 'A:/' + limaReference + '/Acquisitions/' + exhibit +  '/'
        targetPath = rootPath + '/Acquisitions/' + exhibit + '/'
        createDirectory(targetPath)
        # createDirectory(targetPath2)

    targetPath = rootPath + 'Case File/X Ways/'
    createDirectory(targetPath)

    targetPath = rootPath + 'Case File/EnCase/Temp/'
    createDirectory(targetPath)

    targetPath = rootPath + 'Case File/EnCase/Index/'
    createDirectory(targetPath)

    targetPath = rootPath + 'Case File/NetClean/'
    createDirectory(targetPath)

    targetPath = rootPath + 'Export/X Ways Carving/'
    createDirectory(targetPath)

    print('All folders created, script has terminated.')

main_menu()

1 个答案:

答案 0 :(得分:3)

您应该在q==n循环之前定义createDictionary函数。在您调用while True函数的代码中,它仍未定义。