Im creating a DOS clone with python and I have a command cd
which allows you to change directory. The only problem is that if you misspell or type a non-existent directory, the program closes with a traceback error. Im basically looking for it not to completely close the program but instead print
a statement like 'requested_directory' Is not a directory!
and allow you to type in a different directory.
Ive tried a couple things, mainly error handling, but to no prevail. Im assuming that im still not quite understanding error handling or using it incorrectly.
Any help is much appreciated.
This is the code im using to change directories (elif
because i have many more commands. cmd
is a raw input.)
elif 'cd' in cmd:
desired_directory = cmd.split(' ')[1]
if desired_directory == "..":
os.chdir('..')
else:
os.chdir(desired_directory)
This is the output when an incorrect directory is typed in
Traceback (most recent call last):
File "/Users/jrosmac/PycharmProjects/untitled/JDOS/SYS64/jdosos.py", line 47, in <module>
os.chdir(desired_directory)
OSError: [Errno 2] No such file or directory: 'raw_input goes here'