Невозможно получить ожидаемый результат команды «dir» с помощью wexpect на Python.
Мой сценарий wexpect:
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()
Ожидаемый результат моего скрипта (точно такой же, как 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 .
2023/10/07 下午 11:43 ..
2024/03/27 下午 08:43 .arduino-create
2024/03/28 下午 01:23 .arduinoIDE
2024/04/24 下午 04:51 .cache
...
2024/06/21 下午 07:00 Videos
16 File(s) 39,531,783 bytes
27 Dir(s) 21,730,820,096 bytes free
C:\Users\vIIr>
Выход моего скрипта:
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/07/03 上午 10:56 .2023/10/07 下午 11:43 ..2024/03/27 下午 08:43 .arduino-create2024/03/28 下午 01:23 ...2024/06/21 下午 07:00 Videos 16 File(s) 35,370,247 bytes
27 Dir(s) 20,507,512,832 bytes free
C:\Users\vIIr>
Подробнее здесь: https://stackoverflow.com/questions/786 ... -on-python