import os
def enter():
now = os.path.abspath(".")
while True:
s = input()
s = s.strip()
if s == 'exit':
print('exit success!')
break
elif s.startswith('cd'):
fs = s.split()
pre = os.path.join(now,fs[1])
if os.path.isdir(pre):
now = pre
else:
print("can't cd into a file or Directory is not exist!")
elif s.startswith('ls'):
for x in os.listdir(now):
print(x)
else:
print("error cmd. Ex: 'cd path'、'ls','exit' to end of the process!")
稍微做了点改动,dir修改ls
Kevin_No5
import os def enter(): now = os.path.abspath(".") while True: s = input() s = s.strip() if s == 'exit': print('exit success!') break elif s.startswith('cd'): fs = s.split() pre = os.path.join(now,fs[1]) if os.path.isdir(pre): now = pre else: print("can't cd into a file or Directory is not exist!") elif s.startswith('ls'): for x in os.listdir(now): print(x) else: print("error cmd. Ex: 'cd path'、'ls','exit' to end of the process!") 稍微做了点改动,dir修改ls