Reputation: 416
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'\r\n \t \t *****Welcome to SPEC_CONV*****\r\n\tThis program converts spectra between RadWare, Ascii,\r\n\tXtrack (GASPWARE) and Ortec (binary Chn & ASCII Spe) formats,\r\n\tincluding multiple-spectra (<999) Xtrack files, e.g. from AGATA.\r\n\tand can gainmatch spectra.\r\n\t(Ascii means (y) or (x y) data starting from channel zero)\r\n\tComment lines starting with # are ignored at the front of\r\n\tascii spectra. The 1 or 2 col. format is auto-detected.\r\n\r\n 1) to convert RadWare (.spe) ==> Ascii (.txt)\r\n 2) to convert Ascii (.txt) ==> RadWare (.spe)\r\n 3) to convert Ascii (.txt) ==> Xtrack (.spec)\r\n 4) to convert Maestro_Chn (.Chn) ==> Ascii (.txt)\r\n 5) to convert Maestro_Chn (.Chn) ==> RadWare (.spe)\r\n 6) to convert Xtrack (.spec) ==> Ascii (.txt)\r\n 7) to convert Xtrack (.spec) ==> RadWare (.spe)\r\n 8) to convert GENIE (.IEC) ==> RadWare (.spe)\r\n 9) to convert Maestro_Spe (.Spe) ==> RadWare (.spe)\r\n a) to convert Maestro_Spe (.Spe) ==> Ascii (.txt)\r\n g) to gainmatch a RadWare spectrum\r\n 0) Quit\r\n5^J\r\nRead spectrum names from list file (y/n) \r\ny^J\r\nType filename containing list of spectrum file names:\r\nCannot open file: \r\nfile_list\r\n'
As you can see at the very end of this extract it is reading the file name as '\r\nfile_list\r\n' 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'\r\n \t \t *****Welcome to SPEC_CONV*****\r\n\tThis program converts spectra between RadWare, Ascii,\r\n\tXtrack (GASPWARE) and Ortec (binary Chn & ASCII Spe) formats,\r\n\tincluding multiple-spectra (<999) Xtrack files, e.g. from AGATA.\r\n\tand can gainmatch spectra.\r\n\t(Ascii means (y) or (x y) data starting from channel zero)\r\n\tComment lines starting with # are ignored at the front of\r\n\tascii spectra. The 1 or 2 col. format is auto-detected.\r\n\r\n\r\nUnrecognised arguments...usage: spec_conv\r\n or: spec_conv SpectrumFileName\r\n ***File --noediting does not exist\r\n5\r\ny\r\nfile_list\r\n'
Upvotes: 0
Views: 1119
Reputation: 12255
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')
Upvotes: 1