# \[REPL Shell\] Append to stderr

**URL:** <https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [February 18, 2025, 3:28pm UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977 "2025-02-18T15:28:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![encryptedoreo](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/encryptedoreo/32/5098_2.png) [@encryptedoreo](https://forum.codecrafters.io/u/encryptedoreo)\
**Post date:** [February 18, 2025, 3:28pm UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/1 "2025-02-18T15:28:37Z")

</div>

I’m stuck on Stage #UN3.

I’ve tried … (mention what you’ve tried so far).

Here are my logs:

```auto
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.
[tester::#UN3] Running tests for Stage #UN3 (Redirection - Append stderr)
[tester::#UN3] [setup] export PATH=/tmp/strawberry/raspberry/raspberry:$PATH
[tester::#UN3] Running ./your_program.sh
[your-program] $ ls -1 nonexistent >> /tmp/baz/foo.md
[your-program] ls: nonexistent: No such file or directory
[tester::#UN3] ✓ Received error message
[tester::#UN3] ✓ File: /tmp/baz/foo.md is empty
[your-program] $ ls -1 nonexistent 2>> /tmp/baz/qux.md
[your-program] $ cat /tmp/baz/qux.md
[your-program] ls: nonexistent: No such file or directory
[tester::#UN3] ✓ Received redirected file content
[your-program] $ echo "James says Error" 2>> /tmp/baz/quz.md
[your-program] James says Error
[tester::#UN3] ✓ Received redirected file content
[your-program] $ cat nonexistent 2>> /tmp/baz/quz.md
[your-program] $ ls -1 nonexistent 2>> /tmp/baz/quz.md
[your-program] $ cat /tmp/baz/quz.md
[your-program] ls: nonexistent: No such file or directory
[tester::#UN3] Output does not match expected value.
[tester::#UN3] Expected: "cat: nonexistent: No such file or directory"
[tester::#UN3] Received: "ls: nonexistent: No such file or directory"
[your-program] $ 
[tester::#UN3] Assertion failed.
[tester::#UN3] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

```

And here’s a snippet of my code:

```python
import sys
import shutil
import os
import shlex
import contextlib

def main():
    builtins = {"type", "exit", "echo", "pwd", "cd"}

    while True:
        sys.stdout.write("$ ")
        command = sys.stdin.readline().strip().replace('1>', '>')

        cmd = shlex.split(command, posix=True)
        stdout = sys.stdout if ">" not in command else open(cmd[-1], f'{"a" if ">>" in cmd else "w"}+')

        match cmd:
            case ["exit", arg]: sys.exit(int(arg))
            case ["exit"]: sys.exit(0)
            case ["echo", *args]: (stdout if '2>' not in args and '2>>' not in args else sys.stdout).write(f"{' '.join(args if stdout == sys.stdout else args[:-2])}\n")
            case ["type", arg] if arg in builtins: stdout.write(f"{arg} is a shell builtin\n")
            case ["type", arg] if path := shutil.which(arg): stdout.write(f"{arg} is {path}\n")
            case ["type", arg] if arg not in builtins: stdout.write(f"{arg}: not found\n")

            case ["pwd"]: sys.stdout.write(f"{os.getcwd()}\n")
            case ["cd", dir_path] if (path_exists := os.path.exists(expanded := os.path.expanduser(dir_path))): os.chdir(expanded)
            case ["cd", dir_path] if not path_exists: stdout.write(f"cd: {os.path.expanduser(dir_path)}: No such file or directory\n")

            case [fn, *args] if shutil.which(fn): os.system(command)
            case [fn, *args] if not shutil.which(fn): stdout.write(f"{fn}: command not found\n")
        
        if ">" in command:
            stdout.close()

if __name__ == " __main__":
    main()

```

---

<div class="post-metadata">

**Author:** ![encryptedoreo](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/encryptedoreo/32/5098_2.png) [@encryptedoreo](https://forum.codecrafters.io/u/encryptedoreo)\
**Post date:** [February 18, 2025, 3:35pm UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/3 "2025-02-18T15:35:58Z")

</div>

The reason I’m confused is because this code should work. If anyone could help that’d be great!

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [February 18, 2025, 3:40pm UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/4 "2025-02-18T15:40:29Z")

</div>

Hi @encryptedoreo, could you [upload your code to GitHub](https://docs.codecrafters.io/features/publish-to-github) and share the link? It will be much easier to debug if I can run it directly.

---

<div class="post-metadata">

**Author:** ![encryptedoreo](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/encryptedoreo/32/5098_2.png) [@encryptedoreo](https://forum.codecrafters.io/u/encryptedoreo)\
**Post date:** [February 18, 2025, 3:54pm UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/5 "2025-02-18T15:54:16Z")

</div>

Hi @andy1li, I have published my code to GitHub. Could you please help me debug my code?

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [February 18, 2025, 3:58pm UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/6 "2025-02-18T15:58:26Z")

</div>

Sure, mind sharing the link?

---

<div class="post-metadata">

**Author:** ![encryptedoreo](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/encryptedoreo/32/5098_2.png) [@encryptedoreo](https://forum.codecrafters.io/u/encryptedoreo)\
**Post date:** [February 18, 2025, 4:04pm UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/8 "2025-02-18T16:04:33Z")

</div>

> **[GitHub - encryptedoreo/codecrafters-shell-python: A shell made in Python.](https://github.com/encryptedoreo/codecrafters-shell-python)**
>
> A shell made in Python.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [February 18, 2025, 4:37pm UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/9 "2025-02-18T16:37:27Z")

</div>

It seems `os.system` is not handling `2>>` correctly and is treating it as `2>`, which overwrites the file instead of appending to it:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/4/446a35bed4acfd19a502e000cebef8c23658b25e.png)

To avoid this issue, consider using [subprocess.run](https://docs.python.org/3/library/subprocess.html) with `shell=True`.

---

<div class="post-metadata">

**Author:** ![encryptedoreo](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/encryptedoreo/32/5098_2.png) [@encryptedoreo](https://forum.codecrafters.io/u/encryptedoreo)\
**Post date:** [February 18, 2025, 4:43pm UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/11 "2025-02-18T16:43:02Z")

</div>

I still seem to have the same problem after changing it to `subprocess.run(command, shell=True, text=True)`. Now it just lists the directories.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [September 25, 2025, 6:46am UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/13 "2025-09-25T06:46:35Z")

</div>

@encryptedoreo Sorry for delayed response!

Looks like relying on the shell features in Python might not be enough anymore.

You may need to implement some of that behavior manually. Let me know if you’d like a hand with that!

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 1, 2025, 7:28am UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/15 "2025-10-01T07:28:35Z")

</div>

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [October 6, 2025, 7:29am UTC](https://forum.codecrafters.io/t/repl-shell-append-to-stderr/4977/16 "2025-10-06T07:29:29Z")

</div>

Closed on October 6, 2025, 7:29am UTC

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