4

我想从 python 脚本使用 DevCon 重新启动驱动程序。它使用以下命令从命令行工作:

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 所以我试试这个:

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 下,并在系统路径中设置。

我知道这可能是重复的问题,但我在 stackoverflow 上尝试了很多解决方案,但我没有解决这个问题。

4

1 回答 1

2

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

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

  2. 我的代码:

    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))
    
于 2017-06-20T08:53:51.677 回答