Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # попытка создать свою консоль с блекджеком и удобными командами
- # TODO: help, os.mkdir(path, mode=0o777, *, dir_fd=None)
- # os.path.abspath() - преобразование относительного пути в абсолютный
- # os.path.isabs() — возвращает True, если путь абсолютный --------------
- import os
- def commandsRecognizer(inp):
- if inp[0] == 'cd':
- cd(inp)
- elif inp[0] == 'sysinfo':
- sysinfo()
- elif inp[0] == 'dir':
- dir()
- elif inp[0] == 'cdir':
- createDirectory(inp)
- elif inp[0] == 'rdir':
- removeDirectory(inp)
- elif inp[0] == 'open':
- openFile(inp)
- elif inp[0] == 'cfile':
- createFile(inp)
- elif inp[0] == 'rfile':
- removeFile(inp)
- elif inp[0] == 'cmd':
- systemCmd(inp)
- def systemCmd(inp):
- #try:
- inp = ' '.join(inp[1::])
- out = str(os.system(inp)).encode("1251", errors='ignore')
- print(out)
- #except: print('опять ты что-то сломал')
- def sysinfo():
- print(os.name)
- def cd(inp):
- try:
- td = os.getcwd().split('\\') # разделяй и властвуй, директорию на список
- if '-' in inp[1]:
- s = inp[1].count('-') # кол-во отступов назад
- if s >= len(td): s = len(td)-2 # если слишком много минусов - в предпоследнюю
- for i in range(s): # убираем элементы списка и соединяем его в строку через \
- del td[-1]
- td = '\\'.join(td)
- os.chdir(td) # меняем директорию на новую
- elif 'C:' not in inp[1]:
- td.append(inp[1])
- td = '\\'.join(td)
- os.chdir(td)
- except:
- print('что-то пошло не так')
- def dir():
- for i in os.listdir(os.getcwd()):
- if os.path.isfile(i):
- print(i,' ', round(os.path.getsize(i)/1024,4),'KB')
- else: print(i)
- if os.listdir(os.getcwd())==[]:
- print('directory is empty')
- def createDirectory(inp):
- try:
- os.mkdir(os.getcwd()+'\\'+inp[1])
- print('created', inp[1])
- except: print('command syntax error')
- def removeDirectory(inp):
- try:
- os.rmdir(os.path.abspath(inp[1]))
- print('removed', inp[1])
- except: print('directory not found or folder are not empty')
- def openFile(inp):
- try:
- os.startfile(str(inp[1]))
- print('opening', inp[1])
- except: print('file not found')
- def remove(inp):
- try:
- os.remove(str(inp[1]))
- print('removed', inp[1])
- except: print('file not found')
- def createFile(inp):
- try:
- nf = open(inp[1], "w+")
- nf.close()
- except: print('command syntax error')
- def cin(): # Console input, разделяет ввод
- print(os.getcwd(), end=' ') # на список для более удобной работы
- inp = input().split()
- if inp[0] == 'stop': return(True)
- if inp[0] not in commandsList: # проверка наличия команды
- print("unknown command")
- commandsRecognizer(inp) # если есть - узнать какая и запустить функцию
- commandsList = ['stop','cd','sysinfo','dir', 'cdir', 'rdir', 'open', 'cfile', 'rfile', 'cmd']
- #список команд, надо как-то с help сопоставить, TODO, mb словарём сделать
- while True:
- if cin(): break
Advertisement
Add Comment
Please, Sign In to add comment