[Shell][Python] cant find directories shown in path

i am trying to complete #mg5 but it cant find the directories in path works locally:
logs

Submitting changes (commit: b70de94)...

Our test runners might be experiencing downtime: https://status.codecrafters.io

Running tests. Logs should appear shortly...

[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.

[tester::#MG5] Running tests for Stage #MG5 (The type builtin: executable files)
[tester::#MG5] [setup] export PATH=/tmp/bar:$PATH
[tester::#MG5] [setup] export PATH=/tmp/quz:$PATH
[tester::#MG5] [setup] PATH is now: /tmp/quz:/tmp/bar:/usr/local/bin:...
[tester::#MG5] [setup] Available executables:
[tester::#MG5] [setup] - my_exe
[tester::#MG5] Running ./your_program.sh
[your-program] $ type cat
[your-program] Traceback (most recent call last):
[tester::#MG5] ^ Line does not match expected value.
[tester::#MG5] Expected: "cat is /bin/cat"
[tester::#MG5] Received: "Traceback (most recent call last):"
[your-program]   File "<frozen runpy>", line 198, in _run_module_as_main
[your-program]   File "<frozen runpy>", line 88, in _run_code
[your-program]   File "/app/app/main.py", line 64, in <module>
[your-program]     main()
[your-program]     ~~~~^^
[your-program]   File "/app/app/main.py", line 47, in main
[your-program]     for y in os.listdir(os.path.join(x)):
[your-program]              ~~~~~~~~~~^^^^^^^^^^^^^^^^^
[your-program] FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/sbin'
[tester::#MG5] Assertion failed.
[tester::#MG5] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

View our article on debugging test failures: https://codecrafters.io/debug

code

import sys
import os

def typer(base,conduit:type) -> tuple:
    try:
        return (conduit(base),True)
    except:
        return (0,False)

def argparse(args,types:list[type]) -> tuple:
    tmp = []
    failed = [0,False]

    if len(types) > len(args):
        failed = [0,True]

    args = args[:len(types)]

    for idx,i in enumerate(args):
        typed = typer(i,types[idx])
        if typed[1] == False:
            failed[0] = idx
            failed[1] = True
        tmp.append(typed)
    return (tmp,failed)

def main():
    while True:
        sys.stdout.write("$ ")
        cmd = input().split(" ")
        match cmd[0]:
            case "exit":
                args = argparse(cmd[1:],[int])
                if args[1][1] == True or args[0][0][1] == False:
                    print("Argument failure")
                else:
                    exit(args[0][0][0])
            case "echo":
                if len(cmd[1:]) == 0:
                    print()
                else:
                    print(" ".join(cmd[1:]))
            case "type":
                path = os.environ["PATH"].split(":")
                cmds = {}
                for x in path:
                    for y in os.listdir(os.path.join(x)):
                        cmds[y] = os.path.join(x)
                args = argparse(cmd[1:],[str])
                if args[1][1] == True or args[0][0][1] == False:
                    print("Argument failure")
                else:
                    if args[0][0][0] in ["exit","echo","type"]:
                        print(f"{args[0][0][0]} is a shell builtin")
                    if args[0][0][0] in cmds.keys():
                        print(f"{args[0][0][0]} is {cmds[args[0][0][0]]}")
                    else:
                        print(f"{args[0][0][0]}: not found")
            case _:
                print(f"{' '.join(cmd)}: command not found")


if __name__ == "__main__":
    main()

Hey @Zeviraty, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

The problem that you are facing is expected: the folders mentioned in PATH

  • May not exist
  • Maybe empty
  • The current shell process may not have the appropriate access to read those directories

Thus using the ‘try to read, but if it fails then ignore silently’ approach is recommended for every folder.

1 Like