Im using pexpect to output some terminal commands, but im facing the following issue.

The code is

porttype = "Gi"

print
expectSw.sendline ("sh int counters errors | i %.*/1 ") % porttype
expectSw.expect ('.*#')
print expectSw.after

But when I run the code i get ...

Traceback (most recent call last):
  File "./script", line 30, in <module>
    expectSw.sendline ("sh int counters errors | i %.*/1 ") % porttype
TypeError: unsupported operand type(s) for %: 'int' and 'str'

It looks like it maybe trying to calculate the string. Can anyone shed any light onto this ?

Thanks,

Dani AI

Generated

Quick summary and root cause

The TypeError happens because the % formatting operator was applied after the call to expectSw.sendline(), and sendline() returns an integer (the number of bytes written). Python evaluated the function call first, so the expression became int % str (modulo of an int by a string), which raises the TypeError. In Python the % token is used both for numeric modulo and for old‑style string formatting, and evaluation follows normal operator precedence (calls and indexing bind before binary %). (pexpect.readthedocs.io)

What to do (practical fixes)

Always build or format the command string before calling sendline() (or explicitly parenthesize the string expression). Prefer modern formatting for clarity and fewer surprises. Example alternatives (not the original post’s lines):

# using str.format (works on Python 2.7+ and 3.x)
cmd = "sh int counters errors | i {}.*/1".format(porttype)
expectSw.sendline(cmd)
# using an f-string (Python 3.6+)
cmd = f"sh int counters errors | i {porttype}.*/1"
expectSw.sendline(cmd)

For older codebases you can still use % formatting — just do it before calling sendline() so the left operand is a string. See modern-formatting recommendations and history. (peps.python.org)

Debugging & safety tips

  • Print repr(cmd) and type(cmd) before sending to confirm you’re sending a string.
  • Capture the return from sendline() (it’s an int) if you need to check bytes written; don’t try to treat it like the formatted command.
  • If you see a bare print or mismatched parentheses, check whether the script is running under Python 2 vs 3 — that can change how print and string literals behave.
  • As hinted and later confirmed, formatting must happen on the string, not after sendline() has executed.

These checks will prevent the same “unsupported operand type(s) for %: 'int' and 'str'” error and make the sending code clearer and more robust.

Recommended Answers

All 2 Replies

maybe something like this:

porttype = "Gi"
print "sh int counters errors | i %s.*/1 " % porttype

>>> sh int counters errors | i Gi.*/1
commented: You were correct. +4

After some further searching I found the issue. The syntax was a little incorrect it should of been :

expectSw.sendline ("sh int counters errors | i %s.*/1 " % porttype)

Thanks,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.