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

2024-03-28 17:17:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个函数一个是我的主菜单,它允许用户输入一个数字来调用另一个函数。当数字1被选中时,我得到错误“NameError:name'createDirectory'is not defined”。我只是觉得调用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()

Tags: inputmainnotfilemenucaseprintenter
1条回答
网友
1楼 · 发布于 2024-03-28 17:17:49

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

相关问题 更多 >