Python Pexpect插件'/r/n';输入sendline()

Python Pexpect插件'/r/n';输入sendline(),python,subprocess,zsh,pexpect,Python,Subprocess,Zsh,Pexpect,我使用pexpect在Ubuntu20.04的zsh终端上自动运行C程序。 所讨论的程序是一个频谱转换器: 我已经在我的路径中安装了它。我无法在终端中运行“spec_conv”,程序运行正常 当程序启动时,有一组初始选项(0-9)。我需要选择5个。第二个选项是单击“Y”。然后程序要求输入文件名。我有一个名为“file_list”的文件,我在终端中键入该文件,频谱按预期处理 我正试图用python实现这一点的自动化。到目前为止,我的代码是: import pexpect child = pexpe

我使用pexpect在Ubuntu20.04的zsh终端上自动运行C程序。 所讨论的程序是一个频谱转换器:

我已经在我的路径中安装了它。我无法在终端中运行“spec_conv”,程序运行正常

当程序启动时,有一组初始选项(0-9)。我需要选择5个。第二个选项是单击“Y”。然后程序要求输入文件名。我有一个名为“file_list”的文件,我在终端中键入该文件,频谱按预期处理

我正试图用python实现这一点的自动化。到目前为止,我的代码是:

import pexpect
child = pexpect.spawn('spec_conv')
child.sendline('5')
child.sendline('y')
child.sendline('file_list')
print(child.read())
代码似乎无法读取文件。
print(child.read())
的输出是:

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'
输出是相同的

我还尝试通过添加建议的“--noediting”来更改.spawn()输入:

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())
这给出了一个先前失败的输出,我不完全理解其原因:

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'

b'\r\n\t\t****欢迎使用SPEC\u-CONV***\r\n\t此程序在RadWare、Ascii、\r\n\tXtrack(GASPWARE)和Ortec(二进制Chn和Ascii Spe)格式之间转换光谱,\r\n\t包括多个光谱(如果您要手动运行生成的程序,您应该能够看到,当您回答
y/n
问题时,您只需键入
y
,即可立即获得答案,无需回车

因此,您需要发送一个字符,而不是使用
sendline()

child.sendline('y')


谢谢你的回答。我尝试了这个,但出现了超时错误。我用结果更新了我的问题。只有
sendline('y')
应该更改为
send('y')
。其他调用必须保持
sendline()
。Aha是有意义的。我需要send('5')和send('y'))现在这就行了。这个C程序是一些,嗯,有趣的代码。作为一种解决方法,可能更容易修改代码,使其不那么具有交互性-例如,将第192行上的get_mode()调用更改为
md=5;
并在第197行上使用
ans[0]='y'
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'
child.send('y')