The answer to my problem has two parts.
As betontalpfa indicated, I must use his version of wexpect. It can be installed easily:
pip install wexpect
The expect_exact
documentation of Pexpect explains that it uses plain string matching instead of compiled regular expressions patterns in the list. This means the parameters must be properly escaped or the expect_exact
method must be used instead of expect
. It gave me this working code:
import wexpect
import sys
rarexe = "C:\Program Files\WinRAR\Rar.exe"
args = ['a', '-vp', '-v2000000b', 'only_first.rar', 'a_big_file.ext']
child = wexpect.spawn(rarexe, args)
# child.logfile = sys.stdout
rar_prompts = [
"Create next volume \? \[Y\]es, \[N\]o, \[A\]ll",
"\[Y\]es, \[N\]o, \[A\]ll, n\[E\]ver, \[R\]ename, \[Q\]uit",
wexpect.EOF, wexpect.TIMEOUT]
index = child.expect(rar_prompts, timeout=8)
while index < 2:
# print(child.before)
if index == 0:
print("No next volume")
child.sendline("N")
elif index == 1:
print("Overwriting existing volume")
child.sendline("Y")
index = child.expect(rar_prompts, timeout=8)
else:
print('Index: %d' % index)
if index == 2:
print("Success!")
else:
print("Time out!")
And the output gives:
Overwriting existing volume
No next volume
Index: 2
Success!