我正在构建一个有望成为MS-DOS风格的操作系统,但是我在文件夹方面遇到了困难。每当我尝试创建或更改目录时,都会收到一条消息
"Command not found."
我不知道为什么会这样,我真的需要一些帮助。我得到的是:
import os
def cmdLvl2():
print("Use the 'leave' command to shut down the system. Use the 'type' command to start a text editor. You can also type 'clear' to clear the screen. Type 'help -a' for more options.")
tcmdLvl2 = input("~$: ")
if tcmdLvl2 == ("leave"):
quit()
if tcmdLvl2 == ("type"):
typer()
if tcmdLvl2 == ("clear"):
os.system('cls')
if tcmdLvl2 == (""):
dcmdLvl2()
if tcmdLvl2 == ("help -a"):
print("You can use the command 'md' to make a new directory. Use the cd command to access this directory. Additionally, use 'list' to show all sub-folders in your current directory.")
cmdLvl2()
if tcmdLvl2 == ("md"):
directoryName = input("Name of directory: ")
os.mkdir(directoryName)
if not os.path.exists(directoryName):
os.mkdir(directoryName)
dcmdLvl2()
if tcmdLvl2 == ("cd"):
changedDIR = input("Directory name: ")
os.chdir(changedDIR)
if not os.path.exists(changedDIR):
print("Directory not found.")
dcmdLvl2()
if tcmdLvl2 == "list":
os.system('dir')
if tcmdLvl2 != ("leave", "type", "clear", "", "help -a", "cr", "cd", "list"):
print("Command not found.")
dcmdLvl2()
def dcmdLvl2():
tcmdLvl2 = input("~$: ")
if tcmdLvl2 == ("leave"):
quit()
if tcmdLvl2 == ("type"):
typer()
if tcmdLvl2 == ("clear"):
os.system('cls')
if tcmdLvl2 == (""):
dcmdLvl2()
if tcmdLvl2 == ("help"):
cmdLvl2()
if tcmdLvl2 == ("cr"):
directoryName = input("Name of directory: ")
os.mkdir(directoryName)
if not os.path.exists(directoryName):
os.mkdir(directoryName)
dcmdLvl2()
if tcmdLvl2 == ("cd"):
changedDIR = input("Directory name: ")
os.chdir(changedDIR)
if not os.path.exists(changedDIR):
print("Directory not found.")
dcmdLvl2()
if tcmdLvl2 == ("list"):
os.system('dir')
if tcmdLvl2 != ("leave", "type", "clear", "", "help", "help -a", "cr", "cd", "list"):
print("Command not found.")
dcmdLvl2()
def typer():
print("Start typing to get started. Unfortunately, you cannot currently save your files.")
typerCMD = input(" ")
dcmdLvl2()
def CMDLine():
print("Hello, and welcome to your new operating system. Type 'help' to get started.")
cmd = input("~$: ")
if cmd == ("help"):
cmdLvl2()
if cmd == ("leave"):
quit()
if cmd == ("type"):
typer()
if cmd == ("clear"):
os.system('cls')
if cmd == (""):
dcmdLvl2()
if cmd == ("help -a"):
print("You can use the command 'cr' to make a new directory. Use the sd command to access this directory. Additionally, use 'list' to show all sub-folders in your current directory.")
cmdLvl2()
if cmd != ("leave", "type", "clear", "", "help -a"):
print("Command not found.")
dcmdLvl2()
def redirect():
signIn()
def mUserRedirect():
makeUser()
def PwordSignIn():
rPword = input("Password: ")
with open('passwords.txt', 'r') as f:
for line in f:
if rPword == (line):
CMDLine()
else:
print("Incorrect password.")
signIn()
def signIn():
rUname = input("Username: ")
with open('usernames.txt', 'r') as f:
for line in f:
if rUname == (line):
PwordSignIn()
else:
print("Username not found.")
mUserRedirect()
def makeUser():
nUname = input("New username: ")
nPword = input("Create a password for the user: ")
with open('usernames.txt', 'w') as f:
f.write(nUname)
with open('passwords.txt', 'w') as f:
f.write(nPword)
signIn()
print("Create a new user? Warning: This will delete any other users. (Y/N) ")
nUser = input("")
if nUser == ("N"):
signIn()
if nUser == ("n"):
signIn()
if nUser == ("Y"):
makeUser()
if nUser == ("y"):
makeUser()
另外,如果有人能帮我设置文件夹,我希望它只在程序中创建文件夹,而不是在我的基本Windows操作系统中创建文件夹。我怎么能这么做
在您的代码中,您多次检查是否存在如下等式:
或者对于这样的不平等:
首先检查字符串是否相等,在本例中,它将起作用,因为在本例中括号是多余的。但是,在第二种情况下,项(字符串)之间用逗号分隔,对于python来说,逗号是元组的定义
与使用
==
或!=
的元组相比,这并不能满足您的需要。 您的意图似乎是检查命令是否是这些字符串中的任何一个。在这种情况下,使用in
检查命令是否是使用圆括号生成的tuple
中的任何单词所以把支票改成:
相关问题 更多 >
编程相关推荐