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

I’m stuck on Stage #jv1

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:

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

1 Like

Here’s the link to it.

@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.

@andy1li, sorry I forgot how i changed the code

1 Like

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