【发布时间】:2019-05-07 04:55:22
【问题描述】:
如何使这个 unix 命令...在 python3 中工作?
unix 命令
echo 'alter table in db' | zenity --text-info --width 600 --height 300 --title 'has this sql been done?'
上面的命令会弹出一个带有文本的框,然后我可以捕获用户的响应。
在 python3 中,我认为我可以将其写入子进程的标准输入,但我不断收到无法解决的神秘错误
下面是执行此操作的python程序
#!/usr/bin/env python3
import subprocess
cmd = ['zenity', '--text-info', '--width', 600, '--height', 300, '--title', 'has this sql been done?']
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
data='alter table in db'
resp = pipe.communicate(input=data)[0]
这个 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 列表包含整数; 600和300。我想这是你的第一个问题。
标签: python python-3.x subprocess pipe