Shell cat Expected prompt ("$ ") but received ""

I’m stuck on Stage #NI6.

I keep getting this error remote: [tester::#NI6] Expected prompt ("$ ") but received “” and Im not sure why. It occurs when the tester runs the cat command.

remote: [tester::#NI6] Running tests for Stage #NI6 (Quoting - Single quotes)
remote: [tester::#NI6] Running ./your_program.sh
remote: [your-program] $ echo ‘hello script’
remote: [your-program] hello script
remote: [tester::#NI6] ✓ Received expected response
remote: [your-program] $ echo script world
remote: [your-program] script world
remote: [tester::#NI6] ✓ Received expected response
remote: [your-program] $ echo ‘example test’
remote: [your-program] example test
remote: [tester::#NI6] ✓ Received expected response
remote: [tester::#NI6] Writing file “/tmp/qux/f 29” with content “grape pineapple.”
remote: [tester::#NI6] Writing file “/tmp/qux/f 2” with content “blueberry banana.”
remote: [tester::#NI6] Writing file “/tmp/qux/f 99” with content “grape pineapple.”
remote: [your-program] $ cat ‘/tmp/qux/f 29’ ‘/tmp/qux/f 2’ ‘/tmp/qux/f 99’
remote: [your-program] grape pineapple.blueberry banana.grape pineapple.
remote: [tester::#NI6] Expected prompt ("$ ") but received “”
remote: [your-program] $
remote: [tester::#NI6] Assertion failed.
remote: [tester::#NI6] Test failed

here is my code:

import sys
import os
import subprocess
import pathlib
import shlex


def parse_args(args):
    args = shlex.split(args)
    return args
def find_executable_path(cmd):
    paths = os.getenv("PATH", "").split(os.pathsep)
    print(paths)
    for path in paths:
        executablePath = os.path.join(path, cmd)
        if os.path.isfile(executablePath):
            return executablePath
    return None
def cd(args):
    if args ==[]:
        print("cd: missing argument")
        return None
    if len(args) > 1:
        print("cd: too many arguments")
        return None
    path = args[0]
    if path.startswith('~'):
        path = os.path.expanduser(path)

    try:
        os.chdir(path)
    except Exception:
        print(f"cd: {path}: No such file or directory")
def ls(args):
    args=args
    if len(args)>1:
        print("ls: too many arguments")
    else:
        for x in(os.listdir(os.getcwd())):
            print(x, "-bytes:",os.path.getsize(x))

def exit(args):
    args=args
    if len(args)>1:
        print("exit: too many arguments")
    else:
        sys.exit(0)
def pwd(args):
    if len(args)>0:
        print("pwd: too many arguments")
    else:
        print(os.getcwd())

def echo(args):
    print(" ".join(args))
def cat(args):
    if not args:
        print("cat: missing argument")
    else:
        output=""
        for arg in args:
            try:
                with open(arg) as f:
                    output+=f.read()        
            except FileNotFoundError:
                print(f"cat: {arg}: No such file or directory")
                return
        print(output)
                
def type_cmd(args): 
    if len(args)>1:
        print("type: too many arguments")
    elif args in ("echo", "exit", "type", "pwd", "cd"):
        print(f"{args} is a shell builtin")
    else:
        executablePath = find_executable_path(args[0])
        if executablePath:
            print(f"{args[0]}:is {executablePath}")
        else:
            print(f"{args[0]}: not found")
builtin_commands={
    "cd":cd,
    "ls":ls,
    "exit":exit, 
    "pwd":pwd,
    "echo":echo,
    "type":type_cmd,
    "cat":cat
}

def main():



    while True:
        sys.stdout.write(f"$ ")
        full_command = input()
        if(full_command==""):
            continue
        args=parse_args(full_command)
        if args[0] in builtin_commands:
            builtin_commands[args[0]](args[1:])
        else:
            print(f"{args[0]}: command not found")



if  __name__ == "__main__":
    main()

Hi @DylanE415, I tried running your code against the previous stages, but it’s actually no longer passing the #EZ5 (The type builtin: builtins) stage.

Suggestions:

  1. Use our CLI to test against previous stages by running:
codecrafters test --previous
  1. Focus on fixing the early stages first, as later stages depend on them.

For #NI6, our tester is not expecting a newline at the end of the cat output:

def cat(args):
    ...
    else:
        ...
        print(output, end="")

I fixed previous error, needed to have end=“” so it doesn’t add a newline print(output,end=“”)

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.