欢迎光临散文网 会员登陆 & 注册

NewyearDOS(仿系统)

2023-08-11 09:47 作者:新的一year  | 我要投稿

用python做了一个仿系统,欢迎使用,以下是源码:

import os

print ("欢迎,版本号:0.017")


class NewyearDOS:

    def __init__(self):

        self.current_dir = os.getcwd()


    def run(self):

        while True:

            command = input(f"{self.current_dir}> ")

            self.execute_command(command)


    def execute_command(self, command):

        command_parts = command.split()


        if command_parts:

            cmd = command_parts[0].lower()

            args = command_parts[1:]


            if cmd == "cd":

                self.change_directory(args)

            elif cmd == "dir":

                self.list_directory()

            elif cmd == "mkdir":

                self.create_directory(args)

            elif cmd == "rmdir":

                self.remove_directory(args)

            elif cmd == "exit":

                exit()

            elif cmd == "help":

                self.show_help()

            else:

                print("无效的命令")


    def change_directory(self, args):

        if args:

            new_dir = os.path.join(self.current_dir, args[0])

            if os.path.isdir(new_dir):

                self.current_dir = new_dir

            else:

                print("目录不存在")

        else:

            print("缺少目录名")


    def list_directory(self):

        files = os.listdir(self.current_dir)

        for file in files:

            file_path = os.path.join(self.current_dir, file)

            if os.path.isdir(file_path):

                print(f"<DIR>\t{file}")

            else:

                print(f"\t{file}")


    def create_directory(self, args):

        if args:

            new_dir = os.path.join(self.current_dir, args[0])

            try:

                os.mkdir(new_dir)

                print(f"目录 '{args[0]}' 创建成功")

            except:

                print("无法创建目录")

        else:

            print("缺少目录名")


    def remove_directory(self, args):

        if args:

            dir_path = os.path.join(self.current_dir, args[0])

            if os.path.isdir(dir_path):

                try:

                    os.rmdir(dir_path)

                    print(f"目录 '{args[0]}' 删除成功")

                except:

                    print("无法删除目录")

            else:

                print("目录不存在")

        else:

            print("缺少目录名")


    def show_help(self):

        print("可用命令:")

        print("cd <目录> - 切换当前目录")

        print("dir - 列出文件和目录")

        print("mkdir <目录> - 创建新目录")

        print("rmdir <目录> - 删除目录")

        print("exit - 退出程序")

        print("help - 显示可用命令及其描述")



dos = NewyearDOS()

dos.run()

看完了点个赞吧!!!!!!!!!!!!


NewyearDOS(仿系统)的评论 (共 条)

分享到微博请遵守国家法律