[英]Why Python can not execute the cmd command 'tskill'?
通常,我们可以使用Python执行Windows的cmd命令,例如:
os.system('ipconfig')
但是我发现如果使用tskill
不能由Python执行:
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执行?
我发现了根本原因:
我的Python是32位的,而我的PC是Windows7 64位的,因此Python的os.system
无法运行tskill
。 如果我改用Python 64位,则一切正常。
使用taskkill,它可以像tskill一样完成几乎所有工作
但是,如果您想在脚本/代码中坚持使用tskill.exe。 请从提升的命令提示符下运行脚本。 (右键单击cmd.exe并以管理员身份运行它)
os.system('c:\windows\system32\tskill.exe 8684')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.