模拟实现Linux shell程序,实现部分功能。
用户可以输入命令(参数可选,使用空格分隔,格式为:命令字 参数1 参数2 …),程序根据输入的命令及参数,执行对应的动作,并进行输出。
启动后,显示当前目录和用户名(见图)、用户循环输入命令,并打印结果。每次命令执行和结果,都保存在log.txt中。
需实现的命令如下:
命令字 功能 参数
calc 计算用户输入的公式 1个参数,数学公式,如:2+3、3*4/5等
pwd 打印当前工作目录 无
cd 切换工作目录 1个参数,要切换的目的路径
ls 显示当前路径下文件和文件夹名称 无
sort 对用户输入的数字从小到大排序空格输出(如:4 5 6 7的形式输出) 多个参数,每个参数都为数字
log 打印所有日志(除log命令本身) 无
name 修改系统的姓名 1个参数,新姓名
count 统计三体1:地球往事.txt文本词频,输出数量前十的十个词语及个数(见图) 无
exit 输出“再见~”,并退出循环体
须注意:
1.每次用户输入的命令和控制台打印的结果,都需要写入log.txt文件,写入方式为追加
2.程序具有一定的错误处理能力,当用户输入命令格式不正确,或无法识别该命令时,可作错误提示。
3.cd 命令,参数为…/(表示上一级目录)或绝对路径(如:D:\test\test)
4.count功能,可使用jieba库进行分词(此题为选作模块)
txt下载后,需放到与py文件相同目录下。
实现思路:生命多个函数,每个函数实现不同功能,根据用户输入去调用不同的函数
代码如下(示例):
import os
import jieba
代码如下(示例):
while(1):
print(url+"("+username+")>", end="")
instructions = input().split(" ")
if(instructions[0] == "calc"):
str = instructions[1:]
calc(str)
elif(instructions[0] == "pwd"):
pwd()
elif(instructions[0] == "cd"):
str = instructions[1:]
cd(str)
elif(instructions[0] == "ls"):
ls()
elif(instructions[0] == "sort"):
str = instructions[1:]
sort(str)
elif(instructions[0] == "log"):
log()
elif(instructions[0] == "name"):
username=str(instructions[1])
elif(instructions[0] == "count"):
count()
elif(instructions[0] == "exit"):
# 输出“再见~”,并退出循环体
print("再见~")
break
代码如下(示例):
import os
import jieba
# 计算用户输入的公式
def calc(n):
a = open("log.txt", 'a')
a.write('calc ')
for i in n:
print(eval(i))
a.write(i)
a.write('\n')
a.close()
# 打印当前工作目录
def pwd():
a = open("log.txt", 'a')
a.write('pwd '+'\r')
a.close()
print(os.getcwd())
# 切换工作目录
def cd(n):
a = open("log.txt", 'a')
for i in n:
os.chdir(i)
a.write('cd '+i+'\r')
a.close()
url=os.getcwd()
# 显示当前路径下文件和文件夹名称
def ls():
a = open("log.txt", 'a')
a.write('ls '+'\r')
a.close()
for i in os.listdir():
print(i)
# 对用户输入的数字从小到大排序空格输出(如:4 5 6 7的形式输出)
def sort(n):
list = []
a = open("log.txt", 'a')
a.write('sort ')
for i in n:
list.append(int(i))
a.write(i+' ')
a.write('\r')
list.sort()
print(list)
a.close()
# 打印所有日志(除log命令本身)
def log():
a = open("log.txt", 'rt')
list1=a.readlines()
a.close()
for i in list1:
print(i,end="")
# 修改系统的姓名
# def name():
# print("这是name")
# 统计三体1:地球往事.txt文本词频,输出数量前十的十个词语及个数(见图)
def count():
a = open("三体1:地球往事.txt",'rt')
txt = a.read()
a.close()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word, 0) + 1
items = list(counts.items()) # 将键值对转换成列表
items.sort(key=lambda x: x[1], reverse=True) # 根据词语出现的次数进行从大到小排序
for i in range(10):
word, count = items[i]
print("{0:<5}{1:>5}".format(word, count))
a = open("log.txt", 'a')
a.write('count' + '\r')
a.close()
username="Alfred"
url=os.getcwd()
while(1):
print(url+"("+username+")>", end="")
instructions = input().split(" ")
if(instructions[0] == "calc"):
str = instructions[1:]
calc(str)
elif(instructions[0] == "pwd"):
pwd()
elif(instructions[0] == "cd"):
str = instructions[1:]
cd(str)
elif(instructions[0] == "ls"):
ls()
elif(instructions[0] == "sort"):
str = instructions[1:]
sort(str)
elif(instructions[0] == "log"):
log()
elif(instructions[0] == "name"):
username=str(instructions[1])
elif(instructions[0] == "count"):
count()
elif(instructions[0] == "exit"):
# 输出“再见~”,并退出循环体
print("再见~")
break