I am building a terminal simulator in Python, and i have the commands stored in arguments, like:
def ls(a):
for item in os.listdir():
print item
a = input('Command: ')
a = a.split()
The terminal works like this:
- Ask user for input
- Splits the input
- Uses the first part of input to search a function in locals
- If there is one, uses the rest part of input as argument
- Calls the function
And i have a problem with cd command.
def cd(a):
if a == None:
print('cd')
print('Change directories')
else:
os.chdir(os.getcwd() + '/' + a())
It works when you make it enter a folder without spaces, like when you type cd Windows
in the prompt, it works. But the problems start when you try to enter a folder that has spaces in it. When i type, for example, cd Documents Copy
, it either enters the folder Documents if there is one, or crashes.
How i can fix it? I had a thought about turning all called function arguments into one, but i dont know how to do that, and there might be other ways.