2019-09-06 09:52:11 +00:00
|
|
|
'''
|
|
|
|
This is the simplest example. It starts a windows command interpreter (aka. cmd) lists the current
|
|
|
|
directory and exits.
|
|
|
|
'''
|
|
|
|
|
|
|
|
import wexpect
|
|
|
|
|
|
|
|
# Start cmd as child process
|
|
|
|
child = wexpect.spawn('cmd.exe')
|
|
|
|
|
|
|
|
# Wait for prompt when cmd becomes ready.
|
|
|
|
child.expect('>')
|
|
|
|
|
|
|
|
# Prints the cmd's start message
|
2020-01-22 11:23:01 +00:00
|
|
|
print(child.before, end='')
|
|
|
|
print(child.after, end='')
|
2019-09-06 09:52:11 +00:00
|
|
|
|
|
|
|
# run list directory command
|
|
|
|
child.sendline('ls')
|
|
|
|
|
|
|
|
# Waiting for prompt
|
|
|
|
child.expect('>')
|
|
|
|
|
|
|
|
# Prints content of the directory
|
2020-01-22 11:23:01 +00:00
|
|
|
print(child.before, end='')
|
|
|
|
print(child.after, end='')
|
2019-09-06 09:52:11 +00:00
|
|
|
|
|
|
|
# Exit from cmd
|
|
|
|
child.sendline('exit')
|
|
|
|
|
|
|
|
# Waiting for cmd termination.
|
|
|
|
child.wait()
|