问题
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)
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.
Edit: Solved, thanks for everyone's help. Upgrading to 64-bit Python did the trick.
回答1:
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'])
Note that msg.exe has nothing to do with CMD, so there's no reason to use shell=True
.
回答2:
Try calling it as separate arguments:
subprocess.call(['msg', '*', 'Hey'], shell=True)
回答3:
Try putting the complete path of the msg
command. Example as listed
import subprocess
subprocess.call("/usr/local/bin/msg * Hey",shell=True)
来源:https://stackoverflow.com/questions/50651872/cant-execute-msg-and-other-windows-commands-via-subprocess