为什么Python不能执行cmd命令';tskill';?
通常,我们可以使用Python执行Windows的cmd命令,例如:为什么Python不能执行cmd命令';tskill';?,python,Python,通常,我们可以使用Python执行Windows的cmd命令,例如: os.system('ipconfig') 但我发现如果我使用以下命令,Python无法执行tskill: os.system('tskill 8684') 要通过pid终止进程,Python将显示cmd的错误: 'tskill' is not recognized as an internal or external command, operable program or batch file. 但是如果我使用cm
os.system('ipconfig')
但我发现如果我使用以下命令,Python无法执行tskill
:
os.system('tskill 8684')
要通过pid终止进程,Python将显示cmd的错误:
'tskill' is not recognized as an internal or external command, operable program or batch file.
但是如果我使用cmd来运行命令,它会工作得很好
据我所知,tskill.exe
位于C:\Windows\System32中,但该路径不在Python的环境变量中。这可能是原因,但是ipconfig.exe也在System32中,可以执行
那么为什么tskill
不能由os.system或subprocess.Popen执行呢?使用taskkill,它几乎可以完成tskill的所有功能
但是如果您想在脚本/代码中坚持使用tskill.exe。请从提升的命令提示运行脚本。(右键单击cmd.exe并以管理员身份运行)
我找到了根本原因:
我的Python是32位的,而我的PC是Windows7 64位的,因此Python的os.system
无法运行tskill
。如果我改用Python 64位,一切正常。消息来自shell,而不是Python。您有哪个版本的Python?那你的操作系统呢?你的可能复制品可以用taskkill来代替。@Jokab我试过taskkill,也不能运行。
os.system('c:\windows\system32\tskill.exe 8684')