如何在Python中连接两个命令字?

问题描述 投票:0回答:2
import os
blah = 'Umm'

print('blah')
os.system('say blah')
"""so I want to make these two things linked, 
so that whenever I print something, it says that something
"""

我想将这两件事联系起来,这样每当我调用

print()
时,它也会说出我打印的内容。

python
2个回答
1
投票

您需要将其包装在一个函数中,然后它只是一个简单的情况,即查找并用

print
 替换所有 
printSay

import os

def printSay(word):
    print(word)
    os.system('say {word}'.format(word=word))

# Usage
printSay("Hello")

0
投票

您可以将

say
作为子进程运行并将数据写入其标准输入。我在 linux 上用 espeak 做了这个,但我想我的 say 命令是正确的。

import subprocess

say = subprocess.Popen(["say", "-f", "-"], stdin=subprocess.PIPE)

def myprint(*args):
    print(*args)
    say.stdin.write((' '.join(args) + '\n').encode('utf-8'))

myprint("hello there")
© www.soinside.com 2019 - 2024. All rights reserved.