/
Androsik
/
Config-workVerse
Обзор
Документация
Войти
/
Androsik
/
Config-workVerse
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
code-4
67 строк
2 KB
Androsik
code-4
05 окт 2025, 17:42
05 окт 2025, 17:42
de814f3
Код
Авторство
О чём код?
import os import subprocess import sys class Shell: def __init__(self): self.current_dir = '/' def ls(self, args): try: subprocess.run(['ls', *args], check=True) except subprocess.CalledProcessError as e: print(f"Ошибка: {e}", file=sys.stderr) def cd(self, args): if len(args) != 1: print("Ошибка: укажите путь для перехода.", file=sys.stderr) return new_dir = args[0] if new_dir == '..': self.current_dir = os.path.dirname(self.current_dir) elif new_dir == '.': pass else: self.current_dir = os.path.join(self.current_dir, new_dir) try: os.chdir(self.current_dir) except FileNotFoundError: print(f"Ошибка: каталог {self.current_dir} не найден.", file=sys.stderr) self.current_dir = os.getcwd() def cal(self, args): try: subprocess.run(['cal', *args], check=True) except subprocess.CalledProcessError as e: print(f"Ошибка: {e}", file=sys.stderr) def clear(self, args): os.system('clear') def run_command(self, command, args): if command == 'ls': self.ls(args) elif command == 'cd': self.cd(args) elif command == 'cal': self.cal(args) elif command == 'clear': self.clear(args) else: print(f"Ошибка: неизвестная команда '{command}'.", file=sys.stderr) def main(): shell = Shell() while True: try: command = input(f"{shell.current_dir} $ ").strip() if not command: continue parts = command.split() shell.run_command(parts[0], parts[1:]) except KeyboardInterrupt: print("\nЗавершение работы.") break if __name__ == '__main__': main()