Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get output of "dir" command on Windows #66

Open
Minecraft-vIIr opened this issue Jul 2, 2024 · 1 comment
Open

Can't get output of "dir" command on Windows #66

Minecraft-vIIr opened this issue Jul 2, 2024 · 1 comment

Comments

@Minecraft-vIIr
Copy link

Minecraft-vIIr commented Jul 2, 2024

Describe the bug
Can't get the output of dir command on Windows. Either a timeout error is raised or the output is different from the output as cmd.exe (without space warp between columns).

To Reproduce

import wexpect

def run_cmd():
    # Start cmd.exe process
    cmd = wexpect.spawn("cmd.exe")

    # Display the initial prompt
    cmd.expect(">")
    print(cmd.before + ">", end="")

    while True:
        # Read user input
        user_input = input()
        
        # Exit loop if user types "exit"
        if user_input.lower() == "exit":
            cmd.sendline("exit")
            break

        # Send the user input to cmd.exe
        cmd.sendline(user_input+" && (echo commandend1 && echo commandend2) || (echo commandend1 && echo commandend2)")

        # Wait for the command to complete and print its output
        cmd.expect(r"(commandend1 \r\ncommandend2\r\n\r\n(?:[^\\]+\\)*[^\\]*>)")

        # Remove the first line of command output (user input)
        print("\r\n".join(cmd.before.split("\r\n")[1:]))

        prompt = cmd.after.replace("commandend1 \r\ncommandend2\r\n\r\n", "")
        print(prompt, end="")

if __name__ == "__main__":
    run_cmd()

Expected behavior
Expected output (supposed to be exactly the same output as cmd.exe):

Microsoft Windows [Version 10.0.22631.3810]
(c) Microsoft Corporation. All rights reserved.

C:\Users\vIIr>dir
 Volume in drive C is Acer
 Volume Serial Number is C226-92AB

 Directory of C:\Users\vIIr

2024/06/30  下午 07:15    <DIR>          .
2023/10/07  下午 11:43    <DIR>          ..
2024/03/27  下午 08:43    <DIR>          .arduino-create
2024/03/28  下午 01:23    <DIR>          .arduinoIDE
...
2024/05/23  上午 10:33    <DIR>          Pictures
2023/12/03  上午 10:21    <DIR>          Saved Games
2023/10/07  下午 11:11    <DIR>          Searches
2023/10/08  下午 04:16    <DIR>          Tracing
2024/06/21  下午 07:00    <DIR>          Videos
              16 File(s)     34,059,527 bytes
              27 Dir(s)  22,039,977,984 bytes free

C:\Users\vIIr>

Environment:

  • [windows version] Windows 11 Pro 23H2
  • [Python version] 3.10.6
  • [wexpect version] 4.0.0
@Minecraft-vIIr
Copy link
Author

I tried repr() and found that there was no \r\n or \n in the raw output of the child process (cmd), which I thought was a bug in wexpect's way of reading the output. Or should I try to update my python as my python version is 3.10.6?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant