Skip to content
Advertisement

Pexpect inserts ‘/r/n’ into sendline()

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:

JavaScript

The code seems to fail at reading the file. The output of print(child.read()) is:

JavaScript

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:

JavaScript

The output is the same.

I have also tried changing my .spawn() input by adding ‘–noediting’ as suggested:

JavaScript

This gives an earlier failed output I don’t fully understand the cause of:

JavaScript

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

JavaScript

by

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement