# Stuck in #JV1 : " ValueError: I/O operation on closed file"

**URL:** <https://forum.codecrafters.io/t/stuck-in-jv1-valueerror-i-o-operation-on-closed-file/4791>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [February 10, 2025, 4:38pm UTC](https://forum.codecrafters.io/t/stuck-in-jv1-valueerror-i-o-operation-on-closed-file/4791 "2025-02-10T16:38:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pravhesh](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/pravhesh/32/4906_2.png) [@Pravhesh](https://forum.codecrafters.io/u/Pravhesh)\
**Post date:** [February 10, 2025, 4:38pm UTC](https://forum.codecrafters.io/t/stuck-in-jv1-valueerror-i-o-operation-on-closed-file/4791/1 "2025-02-10T16:38:29Z")

</div>

I’m stuck on Stage #jv1

```auto
import sys
import os
import shlex
import subprocess
from typing import Optional

def main():
    PATH = os.getenv("PATH").split(":")
    builtin_cmd = ["echo", "exit", "type", "pwd", "cd"]

    while True:
        sys.stdout.write("$ ")
        sys.stdout.flush()

        command = input().strip()

        if not command:
            continue

        parts = shlex.split(command)

    
        redirect = False
        output_file = None
        if '>' in parts or '1>' in parts:
            try:
                redir_index = parts.index('>') if '>' in parts else parts.index('1>')
                output_file = parts[redir_index + 1]
                parts = parts[:redir_index]  
                redirect = True
            except IndexError:
                print("Syntax error: No output file specified")
                continue

        command = parts[0]  

        # Built-in commands
        if command == "echo":
            output = " ".join(parts[1:])
        
        elif command == "exit" and len(parts) == 2 and parts[1] == "0":
            sys.exit()
        
        elif command == "type":
            cmd_name = parts[1] if len(parts) > 1 else ""
            if cmd_name in builtin_cmd:
                output = f"{cmd_name} is a shell builtin"
            else:
                cmd_path = None
                for path in PATH:
                    full_path = os.path.join(path, cmd_name)
                    if os.path.isfile(full_path) and os.access(full_path, os.X_OK):
                        cmd_path = full_path
                        break
                output = f"{cmd_name} is {cmd_path}" if cmd_path else f"{cmd_name}: not found"
        
        elif command == "pwd":
            output = os.getcwd()
        
        elif command == "cd":
            try:
                target_dir = parts[1] if len(parts) > 1 else os.path.expanduser("~")
                os.chdir(target_dir)
                output = ""  
            except FileNotFoundError:
                output = f"{parts[1]}: No such file or directory"
            except IndexError:
                output = "cd: missing operand"
        
        else:
            
            executable_path = None
            for path in PATH:
                full_path = os.path.join(path, command)
                if os.path.isfile(full_path) and os.access(full_path, os.X_OK):
                    executable_path = full_path
                    break

            if executable_path:
                try:
                    with open(output_file, "w") if redirect else sys.stdout as f:
                        subprocess.run(parts, stdout=f, stderr=sys.stderr)
                    continue  
                except Exception as e:
                    output = f"Error: {e}"
            else:
                output = f"{command}: command not found"

        
        if redirect and output_file:
            output_dir = os.path.dirname(output_file)
            if output_dir and not os.path.exists(output_dir):
                os.makedirs(output_dir)  

            with open(output_file, "w") as f:
                f.write(output + "\n")
        else:
            print(output)

if __name__ == " __main__":
    main()

```

And here’s a snippet of my output:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/b/b76bc4d3fa7fdaa8f1ba2042ac956c7bc0feab1f.jpeg)

```auto

```

---

<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 11, 2025, 12:01am UTC](https://forum.codecrafters.io/t/stuck-in-jv1-valueerror-i-o-operation-on-closed-file/4791/2 "2025-02-11T00:01:03Z")

</div>

Hi @Pravhesh, 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:** ![Pravhesh](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/pravhesh/32/4906_2.png) [@Pravhesh](https://forum.codecrafters.io/u/Pravhesh)\
**Post date:** [February 16, 2025, 8:14am UTC](https://forum.codecrafters.io/t/stuck-in-jv1-valueerror-i-o-operation-on-closed-file/4791/5 "2025-02-16T08:14:06Z")

</div>

> **[GitHub - Pravhesh/codecrafters-shell-python](https://github.com/Pravhesh/codecrafters-shell-python)**
>
> Contribute to Pravhesh/codecrafters-shell-python development by creating an account on GitHub.

Here’s the link to it.

---

<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:** [April 23, 2025, 8:43am UTC](https://forum.codecrafters.io/t/stuck-in-jv1-valueerror-i-o-operation-on-closed-file/4791/7 "2025-04-23T08:43:25Z")

</div>

@Pravhesh Looks like you’ve got past this stage. Do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

---

<div class="post-metadata">

**Author:** ![Pravhesh](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/pravhesh/32/4906_2.png) [@Pravhesh](https://forum.codecrafters.io/u/Pravhesh)\
**Post date:** [May 6, 2025, 5:38am UTC](https://forum.codecrafters.io/t/stuck-in-jv1-valueerror-i-o-operation-on-closed-file/4791/10 "2025-05-06T05:38:36Z")

</div>

@andy1li, sorry I forgot how i changed the code

---

<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:** [May 11, 2025, 9:53pm UTC](https://forum.codecrafters.io/t/stuck-in-jv1-valueerror-i-o-operation-on-closed-file/4791/11 "2025-05-11T21:53:24Z")

</div>

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