python - Python3 Tkinter - Write input (y) to console for subprocess -


i've been looking around answer problem have been unlucky. answer work native python , simple.

my problem i'm using subprocess in tkinter application, 1 of commands require write y/n sure want proceed action.

so i'm looking way write y terminal when message appears: sure want continue? (y/n)

i've tried running subprocess.run("y") doesn't seem work.

i'm testing on debian linux , call command asks if want proceed, subprocess.getoutput() can check errors.

code

class removepublickeydialog: def __init__(self, parent):     top = self.top = toplevel(parent)      label(top, text="who remove?").pack()      self.e = entry(top)     self.e.pack(padx=5)      b = button(top, text="remove", command=self.ok)     b.pack(pady=5)  def ok(self):     #print("value " + self.e.get())     key = self.e.get()     cmd = subprocess.getoutput("gpg --delete-keys " + key)     print(cmd)     if ("key \"" + key + "\" not found" in cmd):         messagebox.showerror("error", "no such public key.")     elif ("delete key keyring?" in cmd):         #subprocess.run("echo 'y'")         messagebox.showinfo("success", "public key \"" + key + "\" deleted keyring.")     else:         messagebox.showerror("error", "unknown error, did input key?")      self.top.destroy() 

this "main" code, works it's need input y proceed.

many command line utilities have flag automatically answers yes prompts - if have access source code of particular command, adding such flag if doesn't have 1 (or making custom version never prompts) may easiest solution. commands automatically if not run directly terminal - sure problem?

if know there single prompt, try:
subprocess.run("echo y | your-command", shell=true)

if there may multiple prompts, you'd have use 1 of more complex options in subprocess module, reading , parsing command output know when reply needed.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -