Python 交互式执行cmd命令
如题,应该用什么库呢?我尝试了pexpect.popen_spawn,代码如下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?')
有人做过类似的吗?这应该是个挺常见的需求。 这应该是个非常不常见的需求。 wp231957 发表于 2020-6-22 10:01
这应该是个非常不常见的需求。
{:10_266:}
看看呗,点击蓝字:
python执行cmd命令的几种方法
python调用cmd命令三种方法 Twilight6 发表于 2020-6-22 10:08
看看呗,点击蓝字:
python执行cmd命令的几种方法
os.system和subprocess这两种方法我知道的,他们都是后台执行cmd命令的,主要问题不是怎么执行cmd命令,是需要做交互啊{:10_324:} FC的注册很坑 发表于 2020-6-22 10:16
os.system和subprocess这两种方法我知道的,他们都是后台执行cmd命令的,主要问题不是怎么执行cmd命令, ...
我之前自己瞎写了一个,你看看行不
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 '))
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.decode('gbk'), resp.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.decode('gbk'), resp.decode('gbk')) FC的注册很坑 发表于 2020-6-22 10:16
os.system和subprocess这两种方法我知道的,他们都是后台执行cmd命令的,主要问题不是怎么执行cmd命令, ...
那我就不知了哈哈,楼上大佬,看看他的 qiuyouzhi 发表于 2020-6-22 10:17
我之前自己瞎写了一个,你看看行不
{:10_275:},果然我是个轮子都用不好的小白{:10_329:},还需要一个sendcontrol的功能,例如ctrl+C打断运行。。 qiuyouzhi 发表于 2020-6-22 10:17
我之前自己瞎写了一个,你看看行不
感觉应该再加个else,检索键盘动作(或者是输入,还没想好,还得试一试),然后执行kill就能实现了吧?{:10_272:} Twilight6 发表于 2020-6-22 10:21
那我就不知了哈哈,楼上大佬,看看他的
活捉大佬,本小白瑟瑟发抖,基础真的太差了{:10_283:} FC的注册很坑 发表于 2020-6-22 10:26
,果然我是个轮子都用不好的小白,还需要一个sendcontrol的功能,例如ctrl+C打断运 ...
那或许需要多线程? qiuyouzhi 发表于 2020-6-22 10:34
那或许需要多线程?
试了下应该是的 FC的注册很坑 发表于 2020-6-22 10:42
试了下应该是的
我刚才看了下,貌似并不需要,就是反应速度有点慢
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 '))
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.decode('gbk'), resp.decode('gbk'))
elif ml[:-1].isalpha() and len(ml) == 2 and ml[-1] == ':':
os.chdir(ml[:-1] + ':\\')
else:
try:
resp = subprocess.Popen(ml, shell = True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
except KeyboardInterrupt:
continue
print(resp.decode('gbk'), resp.decode('gbk'))
qiuyouzhi 发表于 2020-6-22 10:44
我刚才看了下,貌似并不需要,就是反应速度有点慢
好像不行的吧,我写了个while True的死循环脚本,输入命令“Python XX.py”运行这个脚本
会卡在resp = subprocess.Popen(ml, shell = True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()这步的,执行不到下一个while ml != 'exit':循环 FC的注册很坑 发表于 2020-6-22 10:54
好像不行的吧,我写了个while True的死循环脚本,输入命令“Python XX.py”运行这个脚本
会卡在resp = s ...
那就得用多线程了
页:
[1]