从python执行DevCon CMD命令 [英] Executing DevCon CMD command from python

查看:148
本文介绍了从python执行DevCon CMD命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Python脚本使用DevCon重新启动驱动程序。它可以通过以下命令从命令行运行:

I want to restart driver with DevCon from python script. It works from command line with this command:

devcon restart \"sd0007322081041363_kcanv\"

我尝试过此操作:

os.system("devcon restart \"sd0007322081041363_kcanv\"")

,结果为:

'devcon' is not recognized as an internal or external command

我读到os.system已过时,我需要使用subprocess.check_output,所以我尝试这样做:

I read that os.system is obsolete and i need to use subprocess.check_output so i try this:

subprocess.check_output(['devcon', 'restart', '"sd0007322081041363_kcanv"'])

结果:

WindowsError:[Error 2] The system cannot find the file specified

这:

subprocess.check_output('devcon restart "sd0007322081041363_kcanv"', shell=True)

,结果为:

subprocess.CalledProcessError: Command 'devcon restart "sd0007322081041363_kcanv"' returned non-zero exit status 1

,并且:

subprocess.Popen("devcon restart \"sd0007322081041363_kcanv\"", shell=True, stdout=subprocess.PIPE).stdout.read()

结果:

'devcon' is not recognized as an internal or external command

并这样:

try:
    subprocess.check_output('devcon disable "sd0007322081041363_kcanv" /f',shell=True,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

,结果为:

RuntimeError: command 'devcon disable "sd0007322081041363_kcanv" /f' return with errpr (cpde 1): 'devcon' is not recognized as an internal or external command, operable program or batch file

devcon.exe在Windows / System32下并且已在系统中设置路径。

devcon.exe is under Windows/System32 and it is set in system path.

我知道这可能是重复的问题,但是我已经尝试过许多关于stackoverflow的解决方案,但是我无法解决此问题。

I know that this can be duplicate question but I have tried many solution on stackoverflow but i van't resolve this issue.

推荐答案

最后,我想出了一个解决方案。我尝试了很多事情,但这对我有用:

Finally, I came up with a solution. I tried many things but this is what works for me:


  1. 从C:\Windows\System32复制devcon.exe并将其放入C:\Windows\SysWOW64。

  1. copy devcon.exe from C:\Windows\System32 and put it to C:\Windows\SysWOW64.

我的代码:

try:
    subprocess.check_output('C:\\Windows\\SysWOW64\\devcon.exe restart "sd0007322081041363_kcanv" /f',shell=True,stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))


这篇关于从python执行DevCon CMD命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆