Skip to content

Instantly share code, notes, and snippets.

@Jan200101
Last active May 31, 2017 08:00
Show Gist options
  • Save Jan200101/70248b9aba8736e5ca5ca3b96f4947b2 to your computer and use it in GitHub Desktop.
Save Jan200101/70248b9aba8736e5ca5ca3b96f4947b2 to your computer and use it in GitHub Desktop.
simple command prompt example in Python - https://repl.it/IZGR/1
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