答案 0 :(得分:0)
请检查以下代码。它似乎有效。只有一个小变化
cat screen.py
def screen_print(string_to_term):
#get the tty of the parent process using shell commands , how to do it using python
cmd="ps -eaf |awk -v pid=$$ '$2==pid{print \"/dev/\"$6}'"
output,error = subprocess.Popen(cmd, shell=True, executable="/bin/bash", stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
with open(output.rstrip()) as fd:
tty = os.ttyname(fd.fileno())
with open(tty, 'wb') as terminal:
terminal.write(string_to_term)
screen_print('This message is coming from python code\n')