开心姥姥 2019-11-12 05:25 采纳率: 50%
浏览 1771
已采纳

求助:Python的Subprocess.popen()返回的对象的stdin怎么用以下代码为何写入后没反应也不会报错

Python


class Thread(threading.Thread):
    def __init__(self, name, sub):
        threading.Thread.__init__(self)
        self.name = name
        self.sub = sub

    def run(self):
        while True:
            if self.sub.poll() is not None:
                break
            command = bytes(str(input("console:").encode('utf-8')), encoding="utf-8")
            # print(type(command))
                        #下面这句不会报错,也执行了,就是无效,跟没有这句一样为啥
            self.sub.stdin.write(command)
is_input_started = False
while True:
    empty = False
    try:
        enter = str(input('主文件路径:'))
    except SyntaxError:
        empty = True
    if not empty:
        if str(input('确定路径吗?(y,n):')) == 'y':
            break
sub = subprocess.Popen(enter, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    while True:
        if sub.poll() is not None:
            print('Stopped')
            break
        out = sub.stdout.readline()
        if not out == "":
            print(out)
        if not is_input_started:
            thread = Thread.Thread('input', sub)
            thread.start()
            is_input_started = True

输入后并没有任何反应,怎么办
求助:Python的Subprocess.popen()返回的对象的stdin怎么用以下代码为何写入后没反应也不会报错
求助:Python的Subprocess.popen()返回的对象的stdin怎么用以下代码为何写入后没反应也不会报错
运行的程序是长时间一直运行
过程中需要互交于是这个问题就来了

  • 写回答

2条回答 默认 最新

  • lectre 2020-02-20 12:10
    关注

    windows系统上尝试stdin.write(command) 中得command字符串后添加'\r\n',随后调用一下stdin.flush()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了