博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python练习 os模块 实现一个小shell
阅读量:6882 次
发布时间:2019-06-27

本文共 3252 字,大约阅读时间需要 10 分钟。

hot3.png

import sysimport osimport reimport getpass# os.name 操作系统平台 Windows:'nt'|Linux/Unix:'posix'。# os.getenv()/os.putenv()读取和设置环境变量。# os.system()函数用来运行shell命令。# os.linesep 行终止符 Windows:'\r\n'|Linux:'\n'|Mac:'\r'# os.path.split() 返回目录名和文件名的tuple# sys.argv 命令行参数# sys.platform 获得操作系统类型# sys.exit(n) 终止程序# sys.path 搜索模块的路径# sys.modules 已载入模块# sys.stdin,sys.stdout,sys.stderr 标准输入/输出/错误输出# sys.version_info 得到python的版本信息def os_sys_test():    print('os.name: ' + os.name)    print('$PATH: ' + os.getenv('PATH'))    print('os.linesep: ' + os.linesep)    print('filename: ' + sys.argv[0])    print('platform: ' + sys.platform)        print(sys.path)    #print(sys.modules)    print(sys.version_info)    def shell():    #os_sys_test()    heading = '''        #########################################        #       welcome to hakase shell         #                                     #           ©copyright  2016            #                                   #########################################    '''    print(heading)    while True:        inputCmd = re.split('\s+', input(getpass.getuser() + '@' + os.getcwd() + '$ '))        cmd = inputCmd[0]        args = inputCmd[1:]        if cmd in cmdList:            cmdList[cmd](args)def fun_rm(args):    for path in args:        if os.path.isfile(path):            os.remove(path)        elif os.path.isdir(path):            lst = os.listdir(path)            os.chdir(path)            fun_rm(lst)            os.chdir('..')            os.removedirs(path)def fun_echo(args):    for str in args:        print(str, end=' ')    print()def fun_cat(args):    for file in args:        with open(file, 'r') as f:            for line in f:                print(line)def fun_ls(args):    if len(args) == 0:        for file in os.listdir('.'):            print(file)    else:        for path in args:            for file in os.listdir(path):                print(file)def fun_mkdir(args):    for dir in args:        if os.path.exists(dir):            print(dir + ' exist !')        else:            os.mkdir(dir)def fun_cd(args):    for path in args:        os.chdir(path)def fun_pwd(args):        print(os.getcwd())def fun_exit(args):    exit(0)def fun_touch(args):    for file in args:        if os.path.exists(file):            print(file + ' exist !')            pass        else:            with open(file, 'w') as f:                passdef fun_clear(args):    os.system('clear')cmdList = {    'echo' : fun_echo,    'exit' : fun_exit,    'cat'  : fun_cat,    'ls'   : fun_ls,    'cd'   : fun_cd,    'pwd'  : fun_pwd,    'touch': fun_touch,    'mkdir': fun_mkdir,    'rm'   : fun_rm,    'clear': fun_clear};if __name__ == '__main__':    shell()

最简单的方法 只实现shell内部命令 外部命令直接使用外部shell来执行 在Window平台上当然运行不起来..

import os, getpass, redef fun_cd(args):    for path in args:        os.chdir(path)def fun_echo(args):    for str in args:        print(str, end=' ')    print()def fun_exit(args):    exit(0)cmdList = {    'echo' : fun_echo,    'exit' : fun_exit,    'cd'   : fun_cd};while True:    cmdLine = input(getpass.getuser() + '@' + os.getcwd() + '$ ')    cmd = re.split('\s+', cmdLine)    if cmd[0] in cmdList:        cmdList[cmd[0]](cmd[1:])    else:        os.system(cmdLine)

 

 

转载于:https://my.oschina.net/nichijou/blog/753462

你可能感兴趣的文章
从异常堆栈中还原 ProGuard 混淆过的代码
查看>>
A20修改顶部状态栏 禁止跳转设置界面
查看>>
Android--多线程之Handler
查看>>
java synchronized用法
查看>>
MySQL开机自动启动的设置方法
查看>>
Spring中@Autowired注解、@Resource注解的区别
查看>>
迭代器
查看>>
【51CTO学院】搜索V3.0内测公告
查看>>
我的友情链接
查看>>
OneNote 2013:可自动实现图片文转字识别功能(OCR)
查看>>
flex学习笔记 皮肤(五) 为titlewindow标题栏添加可以操作的子项
查看>>
安装numpy+mkl
查看>>
第二章—在HTML文件中添加JavaScript
查看>>
CISCO 4503E启用DHCP
查看>>
dockerfile构建镜像
查看>>
进程绑定CPU简单应用
查看>>
将导航栏始终固定在窗口顶部:
查看>>
手机免流量,还会是天方夜谭吗?
查看>>
DNS服务器全面解析
查看>>
CPU每时钟周期浮点计算值是4还是8
查看>>