Hello, hope everyone’s doing well. I have a question regarding #MG5. I’m currently working on a windows device and I realized after testing that the code requires the answer to be in a posix format? because when I attempt to run my code on my own device it only works if I add the .exe extension, but that means the tests dont pass but without it, the tests do pass.
Here is my entire code:
import sys
import os
def getFirstWordEndIndex(command):
index = command.find(" ")
index = index + 1
return index
def main():
# TODO: Uncomment the code below to pass the first stage
builtinCommands = ["echo", "type", "exit"]
while True:
sys.stdout.write("$ ")
command = input()
index = getFirstWordEndIndex(command)
if command == "exit":
break
elif "echo" in command[:index]:
print(command[index:])
elif "type" in command[:index]:
if command[index:] in builtinCommands:
print(f"{command[index:]} is a shell builtin")
else:
system_path = os.environ.get("PATH")
command = command[index:]
directories = system_path.split(os.pathsep)
found = False
for directory in directories:
if (
os.path.exists(directory)
and command in os.listdir(directory)
and os.access(f"{directory}{os.path.sep}{command}", os.X_OK)
):
found = True
print(f"{command} is {directory}{os.path.sep}{command}")
break
if not found:
print(f"{command}: not found")
else:
print(f"{command}: command not found")
if __name__ == "__main__":
main()
(Yes I know my code isn’t great lol)
So I suppose my question is, is there a way to generalize it so that it works on any OS? Because im not sure a function exists to figure out the exact extension of an executable for each OS and I figured out how I could use os.pathsep and os.path.sep to get around the different delimeters but beyond that I’m a little lost.