在Python脚本中使用命令创建原始输入

在Python脚本中使用命令创建原始输入,python,raw-input,ftplib,Python,Raw Input,Ftplib,我正试图实现一个小脚本,通过命令行和适当的“ftplib”模块在Python中使用FTP连接来管理本地主机。我想为用户创建一种原始输入,但是已经设置了一些命令 我试图更好地解释: 创建FTP连接并通过用户名和密码成功完成登录连接后,我将显示一种“bash shell”,可以使用最著名的UNIX命令(例如cd和ls分别在目录中移动并在当前路径中显示文件/文件夹) 例如,我可以这样做: > cd "path inside localhost" 从而显示目录或: > ls 显示该特定路

我正试图实现一个小脚本,通过命令行和适当的“ftplib”模块在Python中使用FTP连接来管理本地主机。我想为用户创建一种原始输入,但是已经设置了一些命令

我试图更好地解释:

创建FTP连接并通过用户名和密码成功完成登录连接后,我将显示一种“bash shell”,可以使用最著名的UNIX命令(例如
cd
ls
分别在目录中移动并在当前路径中显示文件/文件夹)

例如,我可以这样做:

> cd "path inside localhost"
从而显示目录或:

> ls
显示该特定路径中的所有文件和目录。我不知道如何实现这一点,所以我问你一些建议


我提前非常感谢您的帮助。

听起来命令行界面就是您要问的部分。将用户输入映射到命令的一个好方法是使用字典,在python中,您可以通过在函数名称后面加()来运行对函数的引用。这里有一个简单的例子,告诉你我的意思

def firstThing():  # this could be your 'cd' task
    print 'ran first task'

def secondThing(): # another task you would want to run
    print 'ran second task'

def showCommands(): # a task to show available commands
    print functionDict.keys()

# a dictionary mapping commands to functions (you could do the same with classes)
functionDict = {'f1': firstThing, 'f2': secondThing, 'help': showCommands}

# the actual function that gets the input
def main():
    cont = True
    while(cont):
        selection = raw_input('enter your selection ')
        if selection == 'q': # quick and dirty way to give the user a way out
            cont = False
        elif selection in functionDict.keys():
            functionDict[selection]()
        else:
            print 'my friend, you do not know me. enter help to see VALID commands'

if __name__ == '__main__':
    main()

看看这个模块,您可以使用这个模块执行shell命令,或者是模块?它有listdir()和chdir()函数。@AshwiniChaudhary我在搜索NotnamedWayne解决方案,不过还是要谢谢你的宝贵建议。