python3在将字符串传递给子进程的stdin时遇到了麻烦

python3在将字符串传递给子进程的stdin时遇到了麻烦,python,python-3.x,subprocess,pipe,Python,Python 3.x,Subprocess,Pipe,如何使此unix命令。。。在python3工作 unix命令 上面的命令弹出一个文本框,然后我可以捕获用户的响应 在python3中,我认为我可以将其写入子进程的stdin,但我不断地遇到一些我无法回避的神秘错误 下面是执行此操作的python程序 但是,此python脚本在以下情况下失败: Traceback (most recent call last): pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subpr

如何使此unix命令。。。在python3工作

unix命令 上面的命令弹出一个文本框,然后我可以捕获用户的响应

在python3中,我认为我可以将其写入子进程的stdin,但我不断地遇到一些我无法回避的神秘错误

下面是执行此操作的python程序 但是,此python脚本在以下情况下失败:

Traceback (most recent call last):
    pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE,  stderr=subprocess.STDOUT)
  File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1275, in _execute_child
    restore_signals, start_new_session, preexec_fn)
TypeError: expected str, bytes or os.PathLike object, not int
任何想法都将受到高度赞赏

如下:

cmd = ['zenity', '--text-info', '--width', 600, '--height', 300, '--title', 'has this sql been done?']
应该是这样的:

cmd = ['zenity', '--text-info', '--width', '600', '--height', '300', '--title', 'has this sql been done?']
即使300和600是数字,您仍然可以在命令行中将它们显示为字符串。

/usr/bin/env蟒蛇3 导入子流程

cmd=['zenity'、'-text-info'、'-width'、'600'、'-height'、'300'、'-title'、'这个sql已经完成了吗?']

pipe=subprocess.Popen(cmd,stdout=subprocess.pipe,stdin=subprocess.pipe,stderr=subprocess.stdout)

data='alter table in db'


resp=pipe.communicate(输入=bytearray(数据'utf-8'))[0]

您的第一个命令列表包含INT;600和300。我想这是你的第一个问题。谢谢阿德里安,我有两个问题。这个命令需要在int周围加引号,就像你说的那样,但是我不能只将文本发送到管道的输入端,它必须转换成bytearray(例如,bytearray('plain text','utf-8'),而不仅仅是在unix shell中工作的'plain text')。我相信你使用的是python3。在Python中,您有字符串,它是unicode(没有任何特定的编码)。但是,当您将字符串发送到其他位置(例如保存到文件)时,必须对其进行编码。因此,必须首先将其转换为字节字符串,这是正确的(但对我来说,我将只执行
“string.encode”(“utf-8”)
)。如果您希望将其隐式化:在
Popen()
中,传入
universal\u newlines=True
cmd = ['zenity', '--text-info', '--width', 600, '--height', 300, '--title', 'has this sql been done?']
cmd = ['zenity', '--text-info', '--width', '600', '--height', '300', '--title', 'has this sql been done?']