在pexpect.run()中包含通配符

2024-10-01 15:37:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我的终端输出是

 $ setserial -g /dev/ttyS*
   /dev/ttyS0, UART: unknown, Port: 0x03f8, IRQ: 4
   /dev/ttyS1, UART: unknown, Port: 0x02f8, IRQ: 3
   /dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4 
   /dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3

我想从pexpect.run()得到相同的结果

^{pr2}$

有没有办法在命令中包含“*”??在


Tags: rundev终端portirqunknownpexpectuart
2条回答

我想在documentation of the spawn-question上找到了你问题的答案:

Remember that Pexpect does NOT interpret shell meta characters such as redirect, pipe, or wild cards (>, |, or *). This is a common mistake. If you want to run a command and pipe it through another command then you must also start a shell. For example::

child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > log_list.txt"')
child.expect(pexpect.EOF)

你能试试吗?在

pexpect.run("/bin/bash setserial -g /dev/ttyS*")

我想您想为任何tty调用setserial(通过pexpect)吗?你也许可以这样做:

import glob
import pexpect
for tty in glob.glob("/dev/ttyS*"):
    pexpect.run("setserial -g %s" % tty)

相关问题 更多 >

    热门问题