Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用Python编写交互式shell?_Python_Shell_Python 3.x - Fatal编程技术网

如何用Python编写交互式shell?

如何用Python编写交互式shell?,python,shell,python-3.x,Python,Shell,Python 3.x,是否可以像运行命令行解释器一样运行python脚本,例如: from sys import argv argv=argv[1:] str=''.join(argv) import os def ls(): x=os.listdir(os.getcwd()) for file in x: print(file) if str =='ls': ls() import cmd class HelloWorld(cmd.Cmd): """Simple

是否可以像运行命令行解释器一样运行python脚本,例如:

from sys import argv

argv=argv[1:]
str=''.join(argv)
import os
def ls():
    x=os.listdir(os.getcwd())
    for file in x:
        print(file)
if str =='ls':
    ls()
import cmd

class HelloWorld(cmd.Cmd):
    """Simple command processor example."""

    def do_greet(self, line):
        print ('hello')

    def do_EOF(self, line):
           #this command will make you exit from the shell
        return True

if __name__ == '__main__':
    HelloWorld().cmdloop()
在本例中,我使用argv传递参数,但我是否希望代码的行为类似于命令行解释器并调用我的函数示例:

myscript>ls file.py myscript>

注意:对不起,如果我没有清楚地解释我的想法,英语不是我的母语

更新:有些人说我的问题重复了,但我重复一遍,我想让“交互式shell”不是“如何在Windows中执行Python脚本”

是的,检查一下

基本上,您只需将其添加为文件的第一行

#/usr/bin/env python

同时将文件模式更改为可执行

chmod+x yourfile.py

您将能够以常规命令的形式执行该文件。关键是,在后台,它只为您的文件调用适当的解释程序(使用hashbang,您只需指定所需的解释程序)。

是的,检查一下

基本上,您只需将其添加为文件的第一行

#/usr/bin/env python

同时将文件模式更改为可执行

chmod+x yourfile.py


您将能够以常规命令的形式执行该文件。关键是,在引擎盖下,它只为您的文件调用适当的解释程序(使用hashbang,您只需指定所需的解释程序)。

经过长时间的搜索,我发现了一个名为cmdln的模块,它提供了执行此类任务的最佳功能,例如:

from sys import argv

argv=argv[1:]
str=''.join(argv)
import os
def ls():
    x=os.listdir(os.getcwd())
    for file in x:
        print(file)
if str =='ls':
    ls()
import cmd

class HelloWorld(cmd.Cmd):
    """Simple command processor example."""

    def do_greet(self, line):
        print ('hello')

    def do_EOF(self, line):
           #this command will make you exit from the shell
        return True

if __name__ == '__main__':
    HelloWorld().cmdloop()
当您运行脚本时,它将如下所示:

(Cmd) greet
hello
(Cmd) print
string
(cmd) EOF

然后炮弹就停了,谢谢所有帮助我的人

经过长时间的搜索,我发现了一个名为cmdln的模块,它提供了执行此类任务的最佳功能,例如:

from sys import argv

argv=argv[1:]
str=''.join(argv)
import os
def ls():
    x=os.listdir(os.getcwd())
    for file in x:
        print(file)
if str =='ls':
    ls()
import cmd

class HelloWorld(cmd.Cmd):
    """Simple command processor example."""

    def do_greet(self, line):
        print ('hello')

    def do_EOF(self, line):
           #this command will make you exit from the shell
        return True

if __name__ == '__main__':
    HelloWorld().cmdloop()
当您运行脚本时,它将如下所示:

(Cmd) greet
hello
(Cmd) print
string
(cmd) EOF

然后炮弹就停了,谢谢所有帮助我的人

如果我理解正确,您需要一个类似于您自己版本的使用python的
ls
的函数。这个答案基于上述假设

事实上,人们已经这样做了,例如使用。我建议您使用一个框架来减少开发时间。
如果您想从头开始,您的想法是使用循环等待命令,执行函数,打印,然后等待

如果我理解正确,您需要一个可以使用python实现自己版本的
ls
的函数。这个答案基于上述假设

事实上,人们已经这样做了,例如使用。我建议您使用一个框架来减少开发时间。
如果您想从头开始,想法是使用循环等待命令,执行函数,打印,然后等待

如果您运行Python解释器,您可以导入一个文件并以这种方式运行。这就是你想要的吗?不,我想要的是作为一个外部程序运行脚本,脚本调用你在脚本中创建的函数,但是没有函数的“()”,描述我想要的最好的词是“交互式shell”不是真的,这里我试图使交互式shell不是“如何正确传递参数”伙计们,让我进一步解释一下,当你打开cmd时,你键入ls/dir(取决于操作系统)来运行命令,然后cmd继续执行用户输入的下一个命令,这就是我的意思。如果你运行Python解释器,你可以导入一个文件并按这种方式运行。这就是你想要的吗?不,我想要的是作为一个外部程序运行脚本,脚本调用你在脚本中创建的函数,但是没有函数的“()”,描述我想要的最好的词是“交互式shell”不是真的,这里我试图使交互式shell不是“如何正确传递参数”伙计们,让我进一步解释一下,当你打开cmd时,你键入ls/dir(取决于操作系统)来运行命令,然后cmd继续执行用户输入的下一个命令,这就是我的意思。遗憾的是,我使用的是windows,而不是linux。在这种情况下,它与不完全相同,我试图让interactive shell不“如何正确传递参数”实际上并没有解决用户的问题。他正在尝试制作一个交互式shell。遗憾的是,我使用的是windows,而不是linux。在这种情况下,它与“不”是重复的,在这里,我试图使交互式shell不是“如何正确传递参数”“实际上并没有解决用户的问题。他正在尝试制作一个交互式外壳。