Skip to content

Instantly share code, notes, and snippets.

@jonaslsaa
Created May 3, 2015 16:58
Show Gist options
  • Select an option

  • Save jonaslsaa/c575425fe75504edd182 to your computer and use it in GitHub Desktop.

Select an option

Save jonaslsaa/c575425fe75504edd182 to your computer and use it in GitHub Desktop.
Python OS - Command Line Interface Operating System (needs an folder named 'usr' in same directory)
# -*- coding: utf-8 -*-
print("Loading OS...")
from datetime import datetime
import os
import re
import sys
import cl_apps
current_time = datetime.now().time()
path = os.getcwd()
def sysFiles():
try:
if not os.path.exists(path+r"\usr"):
os.makedirs(path+r"\usr")
logfile = open(path+"\system_log.nfo","a")
logfile.write("\n\nBooted OS "+str(current_time)+"\n"+str(path))
logfile.close()
except:
print("Error while preparing files!")
sysFiles()
print("Loaded OS.\n")
print(" ---- PyClOS 1.2 ---- ")
def main():
command = input(r"\n\nusr§ ")
command = command.lower()
cl_apps.explorer(command, path+r"\usr", path, RunOnce)
RunOnce = False
while True:
current_time = datetime.now().time()
main()
RunOnce = True
# -*- coding: utf-8 -*-
def explorer(command, usrpath, syspath, run):
import os
import re
basic = command.partition(' ')[0]
if run == False:
path = usrpath
logpath=syspath+"\system_log.nfo"
try:
if basic == "mkfile":
try:
arg = command.split(" ")[1]
try:
f = open(path+"\."+arg, "wb")
f.close()
logfile = open(logpath,"a")
logfile.write("\nSuccessfully created file: "+str(arg))
logfile.close()
except:
logfile = open(logpath,"a")
logfile.write("\nCould not create file: "+str(arg))
logfile.close()
print("Error! Could not create file.")
except:
logfile = open(logpath,"a")
logfile.write("\nCould not create file: "+str(arg))
logfile.close()
print("Not valid argument! Example: mkfile file_name")
elif basic == "help":
print("List of commands: \n")
print("mkfile - makes files to usr directory, use: mkfile file_name")
print("read - read created files located in usr directory, use: read file_name")
print("edit - writes and appends to files located in usr directory, use: edit file_name w/a")
print("help - help prints to console all commands and how to use them")
print("ls, dir - shows all files in usr directory")
print("rename - renames files located in usr directory, use: rename old_file_name new_file_name")
print(r"setpath - set your own path. Very Buggy and not quite working! use: setpath C:\\new\path")
print("getpath - prints to screen default usr path and current path.")
print("del, delete, rm, remove - deletes a file in usr directory, use: del file_name\n")
logfile = open(logpath,"a")
logfile.write("\nSuccessfully executed help command.")
logfile.close()
elif basic == "read":
try:
arg1 = command.split(" ")[1]
try:
f = open(path+"\."+arg1, "rb")
r = f.read()
print("Ignore b'<--example text-->' :\n")
print(r)
f.close()
logfile = open(logpath,"a")
logfile.write("\nSuccessfully read file: "+str(arg1))
logfile.close()
except:
logfile = open(logpath,"a")
logfile.write("\nCould not read file: "+str(arg1))
logfile.close()
print("Error! Could not read file.")
except:
logfile = open(logpath,"a")
logfile.write("\nCould not read file: "+str(arg1))
logfile.close()
print("Not valid argument! Example: read file_name")
elif basic == "rename":
try:
arg1 = command.split(" ")[1]
arg2 = command.split(" ")[2]
try:
os.renames(path+"\."+arg1, path+"\."+arg2)
logfile = open(logpath,"a")
logfile.write("\nSuccessfully renamed "+str(arg1)+" to "+str(arg2))
logfile.close()
except:
logfile = open(logpath,"a")
logfile.write("\nSomething went wrong while renaming the file: "+str(arg1)+" and "+str(arg2))
logfile.close()
print("Something went wrong while renaming the file!")
except:
logfile = open(logpath,"a")
logfile.write("\nSomething went wrong while renaming a file!")
logfile.close()
print("Arguments are invalid!, Example: rename old_file_name new_file_name")
elif basic == "edit":
arg1 = command.split(" ")[1]
try:
arg2 = command.split(" ")[2]
print("Write to file:")
if arg2 == "w":
f = open(path+"\."+arg1, "w")
f.write(input(" : "))
logfile = open(logpath,"a")
logfile.write("\nSuccessfully edited file: "+str(arg1))
logfile.close()
elif arg2 == "a":
f = open(path+"\."+arg1, "a")
f.write(input(" : "))
logfile = open(logpath,"a")
logfile.write("\nSuccessfully edited file: "+str(arg1))
logfile.close()
else:
print(r"Argument 2 was invalid. Example: edit file_name w/a")
logfile = open(logpath,"a")
logfile.write("\nFailed to edit file!")
logfile.close()
f.close()
except:
print(r"There was no Argument 2. Example: edit file_name w/a")
logfile = open(logpath,"a")
logfile.write("\nFailed to edit file!")
logfile.close()
elif basic == "ls" or basic == "dir":
files = os.listdir("usr")
print("\n All Files: ")
for i in files:
print(i)
logfile = open(logpath,"a")
logfile.write("\nSuccessfully listed all files in "+str(path))
logfile.close()
elif basic == "del" or basic == "delete" or basic == "rm" or basic == "remove":
try:
arg = command.split(" ")[1]
try:
os.remove(path+r"\."+arg)
except:
print("Failed to delete file!")
logfile = open(logpath,"a")
logfile.write("\nFailed to delete file: "+str(arg))
logfile.close()
except:
print("Argument 1 was invalid!")
logfile = open(logpath,"a")
logfile.write("\nFailed to delete file: "+str(arg))
logfile.close()
elif basic == "getpath":
print("Current path: " + str(path))
print("Usr directory path: " + str(path))
logfile = open(logpath,"a")
logfile.write("\nSuccessfully listed paths!")
logfile.close()
elif basic == "setpath": #Not Working yet.
try:
arg = command.split(" ")[1]
print("\nThis is not recommended! Please confirm.")
confirmation = input("!NOT WORKING! Do you confirm setting path to" + str(path) + " ? y/n: ")
if confirmation.lower() == "y":
try:
path = arg
print("Path Set!\n")
logfile = open(logpath,"a")
logfile.write("\nSuccessfully set a new path: "+str(arg))
logfile.close()
except:
print(r"Error! Couldn't set path.")
logfile = open(logpath,"a")
logfile.write("\nFailed to set a new path: "+str(arg))
logfile.close()
except:
print("Not valid argument! Example: setpath")
logfile = open(logpath,"a")
logfile.write("\nFailed to set a new path!")
logfile.close()
else:
print("This is not an valid command. use command: help")
logfile = open(logpath,"a")
logfile.write("\nInvalid command! "+str(command))
logfile.close()
except:
print(r"Command Failed! Try 'help'")
logfile = open(logpath,"a")
logfile.write("\nInvalid command! "+str(command))
logfile.close()
basic = ""
arg = ""
command = ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment