我正在尝试做一些类似命令行界面的东西。以下是相关代码:
from os import system
import datetime, math
system("title Improvised Shell")
def ver():
with open("files/Version.txt") as file:
cont = file.read()
print("\n" + cont + "\n")
def Help():
with open("files/help.txt", 'r') as file:
cont = file.read()
print(cont)
print("Welcome to Improvised Shell. This makes Command Prompt a whole lot easier(sort of). Type help or '?' to view the list of valid commands. \n")
def main():
while True:
cmd = input("$ ")
if cmd == "help" or cmd == "?":
print("\n")
Help()
elif cmd == "shell":
ver()
elif len(cmd) == 0:
cmd
elif cmd == "cls" or cmd == "clear":
system("cls")
elif cmd == "cd" or cmd == "cud":
system("cd")
elif cmd == "snet":
system("netsh wlan show profiles")
elif cmd == "quit":
quit()
elif cmd == "dir":
path = input(" Path >>$ ")
if len(path) == 0:
print("Invalid Path")
elif path == "quit":
print("--none--")
elif path == "same":
system("dir")
else:
system("dir %s" % path)
elif cmd == "python":
system("python")
elif cmd == "ping":
ip = input("IP $: ")
if len(ip) == 0:
print("Invalid IP Address")
else:
system("ping %s" % ip)
elif cmd == "newWin":
system("start Shell.exe")
elif cmd == "open":
filpth = input("File Path>$ ")
if len(filpth) == 0:
print("Invalid Path")
else:
system("start %s" % filpth)
elif cmd == "ip":
system("ipconfig")
elif cmd[0:8] == "asciiDir":
if cmd[9:11] == '-p':
PTH = cmd[12:]
if len(PTH) == 0:
print("Path was not specified")
else:
gdir = cmd[12:]
print("\n")
system("tree /a %s" % gdir)
print("\n")
else:
print("\n")
system("tree /a")
print("\n")
elif cmd == "txtCont":
txtpath = input(" Path >>$ ")
if len(txtpath) == 0:
print("Invalid path. Please enter a path and try again.")
elif txtpath == "quit":
print("--none--")
else:
print("\n")
print("-------------------------------")
system("type %s" % txtpath)
print("\n")
print("-------------------------------")
print("\n")
elif cmd == "defColor":
system("color")
elif cmd == "chColor":
color = input("Color >>$ ")
if len(color) == 0:
print("Invalid color. Please enter a color and try again.")
elif color == "quit":
print("--none--")
elif color == "black":
system("color 0")
elif color == "blue":
system("color 1")
elif color == "Lblue":
system("color 9")
elif color == "green":
system("color 2")
elif color == "Lgreen":
system("color a")
elif color == "aqua":
system("color 3")
elif color == "Laqua":
system("color b")
elif color == "red":
system("color 4")
elif color == "Lred":
system("color c")
elif color == "purple":
system("color 5")
elif color == "Lpurple":
system("color d")
elif color == "yellow":
system("color 6")
elif color == "Lyellow":
system("color e")
elif color == "white":
system("color 7")
elif color == "Bwhite":
system("color f")
else:
print("Invalid color")
elif cmd == "sysinf":
system("systeminfo")
elif cmd == "time":
system("time /t")
elif cmd == "chtime":
system("time")
elif cmd == "winTitle":
title = input(" Title >>$ ")
if len(title) == 0:
print("Please enter a title and try again.")
elif title == "quit":
print("--none--")
else:
system("title %s" % title)
print("Title of this window has been successfully set to '%s'. \n" % title)
elif cmd == "rename":
filename = input(" File You Want to Rename >>$ ")
if len(filename) == 0:
print("Invalid Filename. Enter a filename to rename and try again.")
elif filename == "quit":
print("--none-- \n")
else:
renfilename = input(" What you want to rename it >>$ ")
system("ren %s %s" % (filename, renfilename))
elif cmd == "copy":
fileSrc = input(" Copy From >>$ ")
copy = input(" Copy To >>$ ")
if fileSrc[-3:] == "all":
system("copy %s*.* %s" % (fileSrc, copy))
else:
system("copy %s %s" % (fileSrc, copy))
elif cmd == "find":
what = input(" What >>$ ")
In = input(" In >>$ ")
if what == "NumOfLines":
system('type %s| find "" /v /c' % In)
else:
system("find /i /n \"%s\" %s" % (what, In))
elif cmd == "eval":
exp = input(" Expression >>$ ")
ans = eval(exp)
answer = str(ans)
print("The answer is: %s" % answer)
elif cmd[0] == ">":
arth = cmd[1:]
Arth = eval(arth)
answer = str(Arth)
print("The Answer Is: " + answer)
elif cmd == "date":
d = datetime.date.today()
day = d.isoweekday()
tomo = day + 1
if day == 1 :
day = "Monday"
if day == 2 :
day = "Tuesday"
if day == 3 :
day = "Wednesday"
if day == 4 :
day = "Thursday"
if day == 5 :
day = "Friday"
if day == 6 :
day = "Saturday"
if day == 7 :
day = "Sunday"
#####################
if tomo == 1 :
tomo = "Monday"
if tomo == 2 :
tomo = "Tuesday"
if tomo == 3 :
tomo = "Wednesday"
if tomo == 4 :
tomo = "Thursday"
if tomo == 5 :
tomo = "Friday"
if tomo == 6 :
tomo = "Saturday"
if tomo == 7 :
tomo = "Sunday"
print("Today: %s, %s/%s/%s" % (day, d.day, d.month, d.year))
print("Tomorrow: %s, %s/%s/%s" % (tomo, d.day+1, d.month, d.year))
elif cmd[0:6] == "search":
kword = cmd[7:]
Kword = kword.replace(' ', '+')
url = "https://www.google.ae/#q=%s&" % Kword
system("start %s" % url)
elif cmd == "run":
run_path = input(" Path >>$ ")
fo = open(run_path)
line = fo.readline()
while line:
cmd = line
line = fo.readline()
if cmd == "help" or cmd == "?":
print("\n")
Help()
elif cmd == "shell":
ver()
elif len(cmd) == 0:
cmd
elif cmd == "cls" or cmd == "clear":
system("cls")
elif cmd == "cd" or cmd == "cud":
system("cd")
elif cmd == "snet":
system("netsh wlan show profiles")
elif cmd == "quit":
quit()
elif cmd == "dir":
path = input(" Path >>$ ")
if len(path) == 0:
print("Invalid Path")
elif path == "quit":
print("--none--")
elif path == "same":
system("dir")
else:
system("dir %s" % path)
elif cmd == "python":
system("python")
elif cmd == "ping":
ip = input("IP $: ")
if len(ip) == 0:
print("Invalid IP Address")
else:
system("ping %s" % ip)
elif cmd == "newWin":
system("start Shell.exe")
elif cmd == "open":
filpth = input("File Path>$ ")
if len(filpth) == 0:
print("Invalid Path")
else:
system("start %s" % filpth)
elif cmd == "ip":
system("ipconfig")
elif cmd[0:8] == "asciiDir":
if cmd[9:11] == '-p':
PTH = cmd[12:]
if len(PTH) == 0:
print("Path was not specified")
else:
gdir = cmd[12:]
print("\n")
system("tree /a %s" % gdir)
print("\n")
else:
print("\n")
system("tree /a")
print("\n")
elif cmd == "txtCont":
txtpath = input(" Path >>$ ")
if len(txtpath) == 0:
print("Invalid path. Please enter a path and try again.")
elif txtpath == "quit":
print("--none--")
else:
print("\n")
print("-------------------------------")
system("type %s" % txtpath)
print("\n")
print("-------------------------------")
print("\n")
elif cmd == "defColor":
system("color")
elif cmd == "chColor":
color = input("Color >>$ ")
if len(color) == 0:
print("Invalid color. Please enter a color and try again.")
elif color == "quit":
print("--none--")
elif color == "black":
system("color 0")
elif color == "blue":
system("color 1")
elif color == "Lblue":
system("color 9")
elif color == "green":
system("color 2")
elif color == "Lgreen":
system("color a")
elif color == "aqua":
system("color 3")
elif color == "Laqua":
system("color b")
elif color == "red":
system("color 4")
elif color == "Lred":
system("color c")
elif color == "purple":
system("color 5")
elif color == "Lpurple":
system("color d")
elif color == "yellow":
system("color 6")
elif color == "Lyellow":
system("color e")
elif color == "white":
system("color 7")
elif color == "Bwhite":
system("color f")
else:
print("Invalid color")
elif cmd == "sysinf":
system("systeminfo")
elif cmd == "time":
system("time /t")
elif cmd == "chtime":
system("time")
elif cmd == "winTitle":
title = input(" Title >>$ ")
if len(title) == 0:
print("Please enter a title and try again.")
elif title == "quit":
print("--none--")
else:
system("title %s" % title)
print("Title of this window has been successfully set to '%s'. \n" % title)
elif cmd == "rename":
filename = input(" File You Want to Rename >>$ ")
if len(filename) == 0:
print("Invalid Filename. Enter a filename to rename and try again.")
elif filename == "quit":
print("--none-- \n")
else:
renfilename = input(" What you want to rename it >>$ ")
system("ren %s %s" % (filename, renfilename))
elif cmd == "copy":
fileSrc = input(" Copy From >>$ ")
copy = input(" Copy To >>$ ")
if fileSrc[-3:] == "all":
system("copy %s*.* %s" % (fileSrc, copy))
else:
system("copy %s %s" % (fileSrc, copy))
elif cmd == "find":
what = input(" What >>$ ")
In = input(" In >>$ ")
if what == "NumOfLines":
system('type %s| find "" /v /c' % In)
else:
system("find /i /n \"%s\" %s" % (what, In))
elif cmd == "eval":
exp = input(" Expression >>$ ")
ans = eval(exp)
answer = str(ans)
print("The answer is: %s" % answer)
elif cmd[0] == ">":
arth = cmd[1:]
Arth = eval(arth)
answer = str(Arth)
print("The Answer Is: " + answer)
elif cmd == "date":
d = datetime.date.today()
day = d.isoweekday()
tomo = day + 1
if day == 1 :
day = "Monday"
if day == 2 :
day = "Tuesday"
if day == 3 :
day = "Wednesday"
if day == 4 :
day = "Thursday"
if day == 5 :
day = "Friday"
if day == 6 :
day = "Saturday"
if day == 7 :
day = "Sunday"
#####################
if tomo == 1 :
tomo = "Monday"
if tomo == 2 :
tomo = "Tuesday"
if tomo == 3 :
tomo = "Wednesday"
if tomo == 4 :
tomo = "Thursday"
if tomo == 5 :
tomo = "Friday"
if tomo == 6 :
tomo = "Saturday"
if tomo == 7 :
tomo = "Sunday"
print("Today: %s, %s/%s/%s" % (day, d.day, d.month, d.year))
print("Tomorrow: %s, %s/%s/%s" % (tomo, d.day+1, d.month, d.year))
elif cmd[0:6] == "search":
kword = cmd[7:]
Kword = kword.replace(' ', '+')
url = "https://www.google.ae/#q=%s&" % Kword
system("start %s" % url)
else:
print("Invalid command!")
main()
我想添加一个名为“run”的命令。它应该做的是,逐行读取文本文件,检查文本文件中的命令是否有效,然后运行命令。我在网上对此做了很多研究,但没有得到正确的答案。我能得到的最接近的方法就是执行文本文件中的最后一行。你知道吗
我对python没有太多经验。 请帮我一把。你知道吗
谢谢你
吉钦仁济
你可以尝试以下方法:
更多关于errors
相关问题 更多 >
编程相关推荐