首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从导入的模块调用函数,然后当我返回到原始模块时,正确使用文本菜单?

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

Stack Overflow用户
提问于 2016-01-28 15:20:32
回答 1查看 48关注 0票数 1

因此,我调用选项[1],该选项带我到createDirectory(),该选项一旦执行其函数,就会将我返回给main_menu()。那么我就不能再次调用选项[1],如何才能再次调用选项[1]

档案1:

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

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-28 20:28:24

您缺少对如何构造和使用模块的基本理解。模块不是“调用”,而是“导入”,以便访问该模块中的函数。最初的post不正确地使用了import语句,而导入语句只应在模块顶部按约定使用一次。

顺便说一句,要小心循环引用,因为您不希望模块A导入模块B,然后再引用模块A,这应该是不必要的。

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

代码语言:javascript
复制
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‘相同的目录中)定义如下:

代码语言:javascript
复制
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...')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35065318

复制
相关文章

相似问题

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