Thursday, December 07, 2017

Python : join with care






Code:

print ("running %s" % ' '.join(cmd))

Error:


Traceback (most recent call last):
  File "test.py", line 29, in <module>
    print ("running %s" % ' '.join(cmd))
TypeError: sequence item 4: expected string, int found


Cause:
There are non-strings in the list cmd.

Ex:
cmd = ["runthis.py", "--host", host, "--port", port]

To make join  happy:
cmd = ["runthis.py", "--host", host, "--port", str(port)]