如何使此unix命令在python3中工作?
echo 'alter table in db' | zenity --text-info --width 600 --height 300 --title 'has this sql been done?'
上面的命令会弹出一个带有文本的框,然后我可以捕获用户的响应。
在python3中,我以为我可以将其写到子进程的stdin中,但是我不断收到无法绕过的神秘错误
#!/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
任何想法都会受到赞赏
答案 0 :(得分:1)
此:
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都是数字,您仍然可以在命令行中将它们显示为字符串。
答案 1 :(得分:0)
导入子进程
cmd = ['zenity','--text-info','--width','600','--height','300','--title','是否已完成此sql ?']
pipe = subprocess.Popen(cmd,stdout = subprocess.PIPE,stdin = subprocess.PIPE,stderr = subprocess.STDOUT)
data ='数据库中的更改表'
resp = pipe.communicate(input = bytearray(data,'utf-8'))[0]