无法通过子进程执行 msg(和其他)Windows 命令 [英] Can't execute msg (and other) Windows commands via subprocess
问题描述
我在使用 subprocess.call()
、subprocess.run()
、subprocess.Popen()
、os.system()
,(以及其他运行命令提示符命令的函数),因为我似乎无法让 msg
命令起作用.
I have been having some problems with subprocess.call()
, subprocess.run()
, subprocess.Popen()
, os.system()
, (and other functions to run command prompt commands) as I can't seem to get the msg
command to work.
import subprocess
subprocess.call("msg * Hey",shell=True)
理论上,不幸的是,这应该向网络上的每台计算机发送嘿",这根本没有运行,我不太确定为什么.我可以在 cmd
上成功运行它,但无法通过 Python 运行它.
In theory, this should send "Hey" to every computer on the network unfortunately, this isn't running at all and I'm not quite sure why. I can run it on cmd
successfully, but can't get it to work from Python.
我很想知道为什么这不起作用,并希望有一种方法来修复我的代码或替代解决方案.
I'd love to hear why this doesn't work and hopeful a way to fix my code or an alternate solution.
已解决,感谢大家的帮助.升级到 64 位 Python 成功了.
Solved, thanks for everyone's help. Upgrading to 64-bit Python did the trick.
推荐答案
在 64 位 Windows 中,msg.exe"仅作为本机 64 位二进制文件分发.如果您使用的是在 WOW64 仿真下执行的 32 位 Python,则不会找到此文件.WOW64 将System32"访问重定向到SysWOW64"目录.在 Windows 7+ 中,使用虚拟SysNative"目录访问真正的System32".例如:
In 64-bit Windows, "msg.exe" is only distributed as a native 64-bit binary. This file won't be found if you're using 32-bit Python, which executes under WOW64 emulation. WOW64 redirects "System32" access to the "SysWOW64" directory. In Windows 7+, use the virtual "SysNative" directory to access the real "System32". For example:
sysroot = os.environ['SystemRoot']
sysnative = os.path.join(sysroot, 'SysNative')
if not os.path.exists(sysnative):
sysnative = os.path.join(sysroot, 'System32')
msgexe_path = os.path.join(sysnative, 'msg.exe')
subprocess.call([msgexe_path, '*', 'Hey'])
注意 msg.exe 与 CMD 无关,所以没有理由使用 shell=True
.
Note that msg.exe has nothing to do with CMD, so there's no reason to use shell=True
.
这篇关于无法通过子进程执行 msg(和其他)Windows 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!