如何从导入的模块调用函数,然后当我返回原始模块时正确使用文本菜单?

时间:2016-01-28 15:20:32

标签: python python-3.x

所以我调用选项[1]将我带到createDirectory(),一旦执行其功能,我就会返回main_menu()。然后,我无法再次调用选项[1],怎样才能获得它,以便我可以再次调用选项[1]

文件1:

import os
from os import makedirs, path
import shutil


def main_menu():
    while True:
            # loop = 1
            # if loop == 1:
                    print("PLEASE CHOOSE AN OPTION BELOW BY ENTERING THE CORRESPONDING NUMBER: \n")

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

                    print("[2]: DELETE X-WAYS CARVING FOLDER")

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

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

                    while True:
                        # choice = int(input("ENTER YOUR CHOICE HERE:"))
                            try:
                                    choice = int(input("ENTER YOUR CHOICE HERE:"))
                                    if choice == 1:
                                            # loop = 0
                                            from CreateDirectory import create_directory
                                            # main_menu()
                                            # break



                                    elif choice == 2:
                                            import RemoveFolder
                                            break

                                    elif choice == 3:
                                            caseBackup()
                                            break

                                    elif choice == 4:

                                            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")
                                    main_menu()

if __name__ == "__main_menu__":
    main_menu()

main_menu()

文件2:

import os
from os import makedirs, path
import sys
sys.path.append('C:/Users/Matthew/Desktop/HTCU Scripts')

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 + '/'
    createDirectory(targetPath)

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.\n')


if __name__ == "__createDirectory__":
    createDirectory(targetPath)

from TestMain import main_menu

1 个答案:

答案 0 :(得分:0)

您错过了对如何构建和使用模块的基本理解。模块不被称为#34;它们是"导入"为了访问该模块中的功能。您的原始帖子不正确地使用了import语句,只能在模块顶部按惯例使用一次。

顺便提一下,请注意循环引用,因为您不希望模块A导入模块B,然后引用回模块A.这绝不是必需的。

因此,您的主模块(在我的示例中名为main.py)可能如下所示:

from myDirUtils import create_folder_structure, remove_folder, case_backup

def main_menu():
    finished = False
    while not finished:
        print("PLEASE CHOOSE AN OPTION BELOW BY ENTERING THE CORRESPONDING NUMBER: \n")
        print("[1]: CREATE CASE FOLDER STRUCTURE")
        print("[2]: DELETE X-WAYS CARVING FOLDER")
        print("[3]: BACK CASE UP TO SERVER")
        print("[4]: CLOSE PROGRAMME \n")
        try:
            choice = int(input("ENTER YOUR CHOICE HERE:"))
            if choice == 1:
                create_folder_structure()
            elif choice == 2:
                remove_folder()
            elif choice == 3:
                case_backup()
            elif choice == 4:
                finished = True
            else:
                print("INVALID CHOICE!! PLEASE ENTER A NUMBER BETWEEN 1-3")
        except ValueError:
            print("INVALID CHOICE!! PLEASE ENTER A NUMBER BETWEEN 1-3")

if __name__ == "__main__":
    # execute only if run as a script
    main_menu()

使用您的第二个模块(名为myDirUtils.py并放在与`main.py'相同的目录中),定义如下:

from os import makedirs, path

def create_folder_structure():
    print('--- all your instructions ---')
    driveLetter = 'D'
    limaReference = 'lima'
    rootPath = driveLetter + ':/' + limaReference + '/'
    # build exhibits here
    targetPath = rootPath + 'CaseFile/XWays/'
    _create_directory(targetPath)
    targetPath = rootPath + 'CaseFile/Encase/Temp'
    _create_directory(targetPath)

def remove_folder():
    print("in remove_folder...")

def case_backup():
    print("in case_backup...")

def _create_directory(targetPath):
    print("in create_directory...")
    if not path.exists(targetPath):
        print('now we have to create the folder')
        # makedirs(targetPath)
    else:
        print('Path ' + targetPath + ' already exists, skipping...')