|  | 
 
| 
如题,应该用什么库呢?我尝试了pexpect.popen_spawn,代码如下
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 复制代码import pexpect
from pexpect import popen_spawn
child=pexpect.popen_spawn.PopenSpawn('python run.py')
child.expect('Input something?')
 但效果是run.py的代码执行了,但是并没有打开控制台,报错:
 pexpect.exceptions.TIMEOUT: <pexpect.popen_spawn.PopenSpawn object at 0x00000264737950B8>
 searcher: searcher_re:
 0: re.compile(b'Input something?')
 <pexpect.popen_spawn.PopenSpawn object at 0x00000264737950B8>
 searcher: searcher_re:
 0: re.compile(b'Input something?')
 
 有人做过类似的吗?这应该是个挺常见的需求。
 
我之前自己瞎写了一个,你看看行不
 复制代码import os
import subprocess
ml = ''
os.chdir("C:\\Users")
while ml != 'exit':
    ml = input(os.getcwd() + '> ')
    if 'cd' in ml:
        try:
            os.chdir(ml.split('cd ')[1])
        except FileNotFoundError as reason:
            print(reason)
            continue
        except:
            resp = subprocess.Popen(ml + ' /?', shell = True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
            print(resp[0].decode('gbk'), resp[1].decode('gbk'))
    elif ml[:-1].isalpha() and len(ml) == 2 and ml[-1] == ':':
        os.chdir(ml[:-1] + ':\\')
    else:
        resp = subprocess.Popen(ml, shell = True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
        print(resp[0].decode('gbk'), resp[1].decode('gbk'))
 | 
 |