I am using pexpect to automate running a C program in a zsh terminal on Ubuntu 20.04. The program in question is a spectrum convertor: http://www.np.ph.bham.ac.uk/research_resources/programs/spec_conv/spec_conv.c
I have this installed and in my path. I can not run ‘spec_conv’ in my terminal and the program runs correctly.
When the program starts there is an initial set of options (0-9). I need to choose 5. The second option I click ‘Y’. The program then asks for a file name. I have a file called ‘file_list’ which I type into the terminal and the spectrum is processed as expected.
I am trying to automate this with python. My code so far is:
import pexpect child = pexpect.spawn('spec_conv') child.sendline('5') child.sendline('y') child.sendline('file_list') print(child.read())
The code seems to fail at reading the file. The output of print(child.read())
is:
b'rn t t *****Welcome to SPEC_CONV*****rntThis program converts spectra between RadWare, Ascii,rntXtrack (GASPWARE) and Ortec (binary Chn & ASCII Spe) formats,rntincluding multiple-spectra (<999) Xtrack files, e.g. from AGATA.rntand can gainmatch spectra.rnt(Ascii means (y) or (x y) data starting from channel zero)rntComment lines starting with # are ignored at the front ofrntascii spectra. The 1 or 2 col. format is auto-detected.rnrn 1) to convert RadWare (.spe) ==> Ascii (.txt)rn 2) to convert Ascii (.txt) ==> RadWare (.spe)rn 3) to convert Ascii (.txt) ==> Xtrack (.spec)rn 4) to convert Maestro_Chn (.Chn) ==> Ascii (.txt)rn 5) to convert Maestro_Chn (.Chn) ==> RadWare (.spe)rn 6) to convert Xtrack (.spec) ==> Ascii (.txt)rn 7) to convert Xtrack (.spec) ==> RadWare (.spe)rn 8) to convert GENIE (.IEC) ==> RadWare (.spe)rn 9) to convert Maestro_Spe (.Spe) ==> RadWare (.spe)rn a) to convert Maestro_Spe (.Spe) ==> Ascii (.txt)rn g) to gainmatch a RadWare spectrumrn 0) Quitrn5^JrnRead spectrum names from list file (y/n) rny^JrnType filename containing list of spectrum file names:rnCannot open file: rnfile_listrn'
As you can see at the very end of this extract it is reading the file name as ‘rnfile_listrn’ so cannot find the file. I have tried a couple of solutions proposed in other similar questions and these have not worked:
https://github.com/pexpect/pexpect/issues/238 Preventing linewrap when using pexpect / bash
Adding setwinsize:
import pexpect child = pexpect.spawn('spec_conv') child.setwinsize(1000,1000) child.sendline('5') child.sendline('y') child.sendline('file_list') print(child.read())
The output is the same.
I have also tried changing my .spawn() input by adding ‘–noediting’ as suggested:
import pexpect child = pexpect.spawn('spec_conv --noediting') child.setwinsize(1000,1000) child.sendline('5') child.sendline('y') child.sendline('file_list') print(child.read())
This gives an earlier failed output I don’t fully understand the cause of:
b'rn t t *****Welcome to SPEC_CONV*****rntThis program converts spectra between RadWare, Ascii,rntXtrack (GASPWARE) and Ortec (binary Chn & ASCII Spe) formats,rntincluding multiple-spectra (<999) Xtrack files, e.g. from AGATA.rntand can gainmatch spectra.rnt(Ascii means (y) or (x y) data starting from channel zero)rntComment lines starting with # are ignored at the front ofrntascii spectra. The 1 or 2 col. format is auto-detected.rnrnrnUnrecognised arguments...usage: spec_convrn or: spec_conv SpectrumFileNamern ***File --noediting does not existrn5rnyrnfile_listrn'
Advertisement
Answer
If you were to run the spawned program manually, you should be able to see that when you reply to the y/n
question you only need to type y
and the answer is taken immediately without the need for a carriage return.
So you need to send a single character, and not use sendline()
which adds a newline to the sent string. Replace
child.sendline('y')
by
child.send('y')