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'
*****Welcome to SPEC_CONV*****
This program converts spectra between RadWare, Ascii,
Xtrack (GASPWARE) and Ortec (binary Chn & ASCII Spe) formats,
including multiple-spectra (<999) Xtrack files, e.g. from AGATA.
and can gainmatch spectra.
(Ascii means (y) or (x y) data starting from channel zero)
Comment lines starting with # are ignored at the front of
ascii spectra. The 1 or 2 col. format is auto-detected.
1) to convert RadWare (.spe) ==> Ascii (.txt)
2) to convert Ascii (.txt) ==> RadWare (.spe)
3) to convert Ascii (.txt) ==> Xtrack (.spec)
4) to convert Maestro_Chn (.Chn) ==> Ascii (.txt)
5) to convert Maestro_Chn (.Chn) ==> RadWare (.spe)
6) to convert Xtrack (.spec) ==> Ascii (.txt)
7) to convert Xtrack (.spec) ==> RadWare (.spe)
8) to convert GENIE (.IEC) ==> RadWare (.spe)
9) to convert Maestro_Spe (.Spe) ==> RadWare (.spe)
a) to convert Maestro_Spe (.Spe) ==> Ascii (.txt)
g) to gainmatch a RadWare spectrum
0) Quit
5^J
Read spectrum names from list file (y/n)
y^J
Type filename containing list of spectrum file names:
Cannot open file:
file_list
'
As you can see at the very end of this extract it is reading the file name as '
file_list
' 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'
*****Welcome to SPEC_CONV*****
This program converts spectra between RadWare, Ascii,
Xtrack (GASPWARE) and Ortec (binary Chn & ASCII Spe) formats,
including multiple-spectra (<999) Xtrack files, e.g. from AGATA.
and can gainmatch spectra.
(Ascii means (y) or (x y) data starting from channel zero)
Comment lines starting with # are ignored at the front of
ascii spectra. The 1 or 2 col. format is auto-detected.
Unrecognised arguments...usage: spec_conv
or: spec_conv SpectrumFileName
***File --noediting does not exist
5
y
file_list
'
question from:
https://stackoverflow.com/questions/65908640/pexpect-inserts-r-n-into-sendline