赞
踩
最近对MS-DOS系统比较感兴趣,想自己做一个,于是我用Python做了一个MS-DOS系统。(很简单,没有任何复杂代码)
1.工具
代码编辑器:Visual Studio Code,这是官网链接。
2.正式开始
2.1.准备
2.1.1.在桌面新建一个文件夹,命名为MS-DOS。当然名字可以自己定。
2.1.2.打开VSCode,选择Open Folder,打开MS-DOS文件夹。
2.1.3.把鼠标放到文件夹上,点击新建文件按钮,新建main.py。
2.2.编写main.py
2.2.1.首先,我们导入好需要的库。
import pandas as pd # 输入DIR命令时读取csv文件
import os # 判断文件和目录是否存在
import sys # 退出程序时使用
import functions as func # 这是导入自己写的函数文件,后面会提到
2.2.2.因为我们要将它做得很高大上,所以我放了欢迎界面。代码如下:
welcome = '''Disk formatted with WinImage 4.00 (c) 1993-97 Gilles Vollant Bootsector from C.H Hochstatter No Systemdisk. Booting from harddisk. Starting MS-DOS 7.1... Welcome to MS-DOS 7.10... Copyright Microsoft Corp. All rights reserved. Killer v1.0 Copyright 1995 Vincent Penquerc'h. All rights reserved. Killer installed in memory. DOSKEY installed. DOSLFN 0.32o: loaded consuming 11840 bytes. SHARE v7.10 (Revision 4.11.1492) Copyright (c) 1989-2003 Datalight, Inc. installed. CuteMouse v1.9.1 [DOS] Installed at PS/2 port Now you are in MS-DOS 7.10 prompt. Type 'help' for help.''' # 字符串太长,所以要用三个单引号 print(welcome) # 显示欢迎内容 content = input('C:\>') # 显示命令提示符
2.2.3.实现ECHO命令
ECHO命令会将文本打印在终端,很好实现,可以直接用print()
语句
if 'ECHO' in content and content.index('E') == 0: # 判断是否为ECHO命令,且字符E在最开头
content.upper() #转换为大写
answer = content.replace('ECHO ', '') # 获取ECHO所要显示的内容
func.write_log(content, answer) # 写入日志文件
print(answer) #打印出内容
else:
print('Bad command or file name') # 输入错误时显示
replace()
函数可以将字符串中的字符替换掉。第一个参数是要替换的字符串,第二个参数是替换成的字符串。这里就是将“ECHO ”以外的字符保留(或者说把“ECHO ”去掉),就得到了想要打印的文本了。
这里我写了一个写入日志文件的函数。我们先创建一个日志文件,名叫history.log,我们要将内容写入日志文件。在这里,我们新建一个文件,命名为functions.py,用来写函数。
写入日志文件:
import time
def write_log(input_string, output_string):
with open(r'C:\Users\Eric\Desktop\Python\Operating_System\MS-DOS\Storage\history.log', 'a') as f: # 这里填自己的路径,右键单击,history.log,选择Copy path,再粘贴到这里。前面一定加r。第二个参数是模式设置,a是追加模式。
time_now = time.strftime('%Y-%m-%d %H:%M:%S') # 获取时间(年-月-日 时-分-秒)。这里变量名千万不能为time,要不然会识别错误。
f.write(f'{time_now}|input:{input_string}|output:{output_string}\n') # f是format的意思,大括号里填变量名称。\n是换行功能。
2.2.4.实现EXIT命令
我们在退出时使用的函数可以直接用sys.exit(0)
实现。
while True:
if 'ECHO' in content and content.index('E') == 0: # 判断是否为ECHO命令,且字符E在最开头
content.upper() #转换为大写
answer = content.replace('ECHO ', '') # 获取ECHO所要显示的内容
func.write_log(content, answer) # 写入日志文件
print(answer) #打印出内容
elif 'EXIT' in content and content.index('E') == 0:
content.upper()
func.write_log(content, 'exit|0')
print('\n')
else:
print('Bad command or file name') # 输入错误时显示
修改一下代码。加上while True:
就可以一直判断。
2.2.5.完整代码:
# file:main.py import pandas as pd import os import sys import functions as func welcome = '''Disk formatted with WinImage 4.00 (c) 1993-97 Gilles Vollant Bootsector from C.H Hochstatter No Systemdisk. Booting from harddisk. Starting MS-DOS 7.1... Welcome to MS-DOS 7.10... Copyright Microsoft Corp. All rights reserved. Killer v1.0 Copyright 1995 Vincent Penquerc'h. All rights reserved. Killer installed in memory. DOSKEY installed. DOSLFN 0.32o: loaded consuming 11840 bytes. SHARE v7.10 (Revision 4.11.1492) Copyright (c) 1989-2003 Datalight, Inc. installed. CuteMouse v1.9.1 [DOS] Installed at PS/2 port Now you are in MS-DOS 7.10 prompt. Type 'help' for help.''' print(welcome) while True: content = input('C:\>') if 'ECHO' in content and content.index('E') == 0: # 判断是否为ECHO命令,且字符E在最开头 content.upper() #转换为大写 answer = content.replace('ECHO ', '') # 获取ECHO所要显示的内容 func.write_log(content, answer) # 写入日志文件 print(answer) #打印出内容 elif 'EXIT' in content and content.index('E') == 0: content.upper() func.write_log(content, 'exit|0') print('\n') else: print('Bad command or file name') # 输入错误时显示
import time
def write_log(input_string, output_string):
with open(r'C:\Users\Eric\Desktop\Python\Operating_System\MS-DOS\Storage\history.log', 'a') as f:
time_now = time.strftime('%Y-%m-%d %H:%M:%S')
f.write(f'{time_now}|input:{input_string}|output:{output_string}\n')
2.2.6.今天就写这么多了,下期再见!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。