Python:windows 上的子进程 + isql:需要类似字节的对象,而不是 'str'

Python: subprocess + isql on windows: a bytes-like object is required, not 'str'

这是公司配的笔记本电脑,我不能在上面安装任何新软件。不过,我可以将 Python 模块安装到我自己的目录中。我需要 运行 Sybase 上的一些东西,而且有 10 多台服务器。手动操作非常耗时,因此我正在寻找选项 Python + subprocess.

我做了一些研究并参考了 using subprocess module in python to run isql command。但是,我的版本不起作用。错误信息是 TypeError: a bytes-like object is required, not 'str'。此错误消息从“通信”行弹出。

我可以看到我的“isql”已成功连接,因为我可以获得 isql.pid。

我错过了什么吗?

谢谢

import subprocess
from subprocess import Popen, PIPE
import keyring
from textwrap import dedent

server = "MYDB"
cmd = r"C:\Sybase15\OCS-15_0\bin\isql.exe"
interface = r"C:\Sybase15\ini\sql.ini"
c = keyring.get_credential("db", None)
username = c.username
password = c.password

isql = Popen([cmd, '-I', interface,
              '-S', server,
              '-U', username, 
              '-P', password,
              '-w', '99999'], stdin=PIPE, stdout=PIPE)
output = isql.communicate("""\
    SET NOCOUNT ON
    {}
    go
""".format("select count(*) from syslogins"))[0]

来自communicate()documentation,

If streams were opened in text mode, input must be a string. Otherwise, it must be bytes.

默认情况下,流以二进制格式打开,但您可以在 Popen() 调用中使用 text=True 参数将其更改为文本模式。