首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用文本菜单从另一个函数调用函数

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

Stack Overflow用户
提问于 2016-01-25 15:13:55
回答 1查看 72关注 0票数 0

我有两个函数,一个是我的main_menu,它允许用户输入一个数字来调用另一个函数。当选择数字1时,我会得到错误"NameError: name‘createDirectory“未定义”。我只是觉得这和调用函数createDirectory一样容易,但显然不是。谁能给我指明正确的方向吗?

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-25 15:17:23

您应该在createDictionary循环之前定义while True函数。在您的代码中,当您调用createDictionary函数时,仍然没有定义它。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34995909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档