Last active
May 31, 2017 08:00
-
-
Save Jan200101/70248b9aba8736e5ca5ca3b96f4947b2 to your computer and use it in GitHub Desktop.
simple command prompt example in Python - https://repl.it/IZGR/1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import platform | |
import getpass | |
from os import getcwd, popen, chdir | |
import socket | |
validos = ['windows', 'linux'] | |
def cmd(*, os:str=None): | |
running = True | |
while running == True: | |
if not cmd: | |
continue | |
if not os or os.lower() not in validos: | |
os = platform.uname()[0].lower() | |
if os.lower() == 'windows': | |
path = getcwd() | |
prefix = '{path}>'.format(path=path) | |
elif os.lower() == 'linux': | |
path = getcwd() | |
user = getpass.getuser() | |
system = socket.gethostname() | |
prefix = '{user}@{system}:{path}$'.format(user=user, system=system, path=path) | |
command = input(prefix) | |
if command.startswith('cd ') and command.split('cd ')[1]: | |
try: | |
chdir(command.split('cd ')[1]) | |
except: | |
print('Invalid Directory') | |
continue | |
if command != 'exit': | |
print(popen(command).read()) | |
else: | |
running = False | |
cmd() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment