Cant execute msg (and other) Windows commands via subprocess

2024/10/8 0:34:16

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.

Answer

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.

https://en.xdnf.cn/q/70184.html

Related Q&A

Django development server stops after logging into admin

I have installed django 3.0 in python 3.7 and started a basic django project. I have created a superuser and run the development server using python manage.py runserver. When I go to localhost:8000/adm…

fastai.fastcore patch decorator vs simple monkey-patching

Im trying to understand the value-added of using fastais fastcore.basics.patch_to decorator. Heres the fastcore way: from fastcore.basics import patch_toclass _T3(int):pass@patch_to(_T3) def func1(self…

Adding user to group on creation in Django

Im looking to add a User to a group only if a field of this User is specified as True once the User is created. Every User that is created would have a UserProfile associated with it. Would this be the…

imgradient matlab equivalent in Python

I am searching for an imgradient MATLAB equivalent in Python. I am aware of cv2.Sobel() and cv2.Laplacian() but it doesnt work as imgradient works in MATLAB. If I could get source code of imgradient.m…

Error: astype() got an unexpected keyword argument categories

df = pd.DataFrame([A+, A, A-, B+, B, B-, C+, C, C-, D+, D],index=[excellent, excellent, excellent, good, good, good, ok, ok, ok, poor, poor])df.rename(columns={0: Grades}, inplace=True)dfI am trying to…

python: regular expression search pattern for binary files (half a byte)

I am using the following regular expression pattern for searching 0xDEAD4FAD in a binary file:my_pattern = re.compile(b"\xDE\xAD\x4F\xAD")but how do I generalize the search pattern for search…

pandas: selecting rows in a specific time window

I have a dataset of samples covering multiple days, all with a timestamp. I want to select rows within a specific time window. E.g. all rows that were generated between 1pm and 3 pm every day.This is a…

How to visualize (dendrogram) a dictionary of hierarchical items?

This is my first time of doing visualization from hierarchical data in dictionary format with Python. Last part of the data looks like this:d = {^2820: [^391, ^1024], ^2821: [^759, w, ^118, ^51], ^2822…

How to unfocus (blur) Python-gi GTK+3 window on Linux

What I want to do and whyI want my window to unfocus, so the previous focused window is selected.Why? I want to interact with the previously selected window (from other programs). My current plan is: …

SyntaxError: multiple exception types must be parenthesized

I am a beginner and have a problem after installing pycaw for the audio control using python, on putting the basic initialization code for pycaw, i get the following error:- Traceback (most recent call…