【超简单,不到一百行】操作系统课设-文件管理

效果:

用python实现了简单的指令:mkdir(创建目录)、ls(查看目录下所有文件)、cd(访问目录)、touch(创建文件)、rm (删除目录或者文件)

代码如下:

TYPE_DIR=0
TYPE_DOC=1

class MyFile:
    fileType:int
    fileName:str
    child:dict
    parent=None
    def __init__(self,name:str,parent,fileType:int) -> None:
        self.child=dict()
        self.parent=parent
        self.fileName=name
        self.fileType=fileType
        self.child[".."]=parent
    def __repr__(self) -> str:
        return self.fileName
    def __str__(self) -> str:
        return self.fileName
    def getPath(self)->str:
        pass
Home=MyFile("home",None,TYPE_DIR)
def analysis_args(args:list[str],workspace:MyFile):
    try:
        command=args[0]
        if(command=="cd"):
            if(len(args)!=2 ):
                raise Exception
            child:MyFile=workspace.child.get(args[1])
            if(child==None):
                print("No such file or directory")
                raise Exception
            elif(child.fileType!=TYPE_DIR):
                print("Not a directory")
                raise Exception
            else:
                return 0,child
        elif(command=="ls"):
            if(len(args)!=1):
                raise Exception
            for file in workspace.child:
                if(file!=".."):
                    print(file)
        elif(command=="mkdir"):
            workspace.child[args[1]]=MyFile(args[1],workspace,TYPE_DIR)
        elif(command=="rm"):
            child:MyFile=workspace.child.get(args[1])
            if(child!=None):
                workspace.child.pop(args[1])
            else:
                print("No such file or directory")
                raise Exception
        elif(command=="touch"):
            child:MyFile=workspace.child.get(args[1])
            if(child==None):
                workspace.child[args[1]]=MyFile(args[1],workspace,TYPE_DOC)
        elif(command=="exit"):
            if(len(args)!=1):
                raise Exception
            else:
                return 1,None
        else:
            raise Exception
    except:
        print("Illegal import!")

    return 0,None

if __name__ == "__main__":
    workspace:MyFile=Home
    path=""
    while(True):
        print("Myos@mamawhes:~/{}{}$".format(path,workspace),end=" ")
        input_str=input()
        #print(input_str)
        args=input_str.split(" ")
        flag,child=analysis_args(args,workspace)
        if(child!=None):
                if(args[1]==".." and args[0]=="cd"):
                    path=path[:len(path)-len(child.fileName)-1]
                else:
                    path+=workspace.fileName+"/"
                workspace=child        
        if(flag==1):
            break
        

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值