1

I have a batch file "test_msg_bat.bat" which works fine from command line but returns the error below from python subprocess popen.

Error: 'msg' is not recognized as an internal or external command, operable program or batch file.

Here is the python code:

import subprocess, os
env = os.environ
p = subprocess.Popen("test_msg_bat.bat", env=env)

Batch file ("test_msg_bat.bat"):

type C:\abcd.txt | msg /time:60 /w %username%

OS: Windows 7 (64 bit)

Python version: 2.6.6 (32 bit)

Edit: This works when using 64-bit python

vjayky
  • 1,082
  • 10
  • 15
  • The error message you get occurs when batch doesn't recognize the command you use. Since msg is only included in the Business and Ultimate versions of Windows 7, I'm guessing that you're not using one of those versions and had to download msg.exe and that msg.exe isn't in the correct place. – SomethingDark Nov 10 '14 at 22:25
  • test this in command prompt before you get going downloading and messing with things: `msg * test` if you get a popup with 'test' in it then msg is working, if not then you'll need to do as SomethingDark said – stoves Nov 10 '14 at 22:26
  • Does the python script work when you use the full path to msg.exe in the batch file? – SomethingDark Nov 10 '14 at 22:44
  • @SomethingDark I am using business version of Windows 7 and haven't downloaded or moved msg.exe. – vjayky Nov 10 '14 at 22:45
  • @stoves "msg * test" works from command line. But does not work when I try os.system("msg * test"). I get the same error message "'msg' is not recognized..." – vjayky Nov 10 '14 at 22:50
  • @SomethingDark Full path of msg.exe does not work. – vjayky Nov 10 '14 at 22:53
  • just run the first two lines and then tell us what `print env` shows – stoves Nov 10 '14 at 23:01
  • @stoves I just tried os.system("C:\\Windows\\System32\\msg.exe * test") on 64 bit version of python 2.6 and it works – vjayky Nov 10 '14 at 23:10

2 Answers2

2

Windows file system redirector causes this error. Refer to the link below for details on how to fix this. Python 32 bit - 64 bit issue: http://bugs.python.org/issue13931

Solution: http://code.activestate.com/recipes/578035-disable-file-system-redirector/

vjayky
  • 1,082
  • 10
  • 15
0

I think something else might be going wrong here as I just made a new .bat file with identical contents and left it on my desktop, then ran your python code and had no problems. Edit: Also had to create an empty abcd.txt file on my desktop for this to work.

import subprocess, os
env = os.environ
p = subprocess.Popen("C:/Users/Stoves/Desktop/test_msg_bat.bat", env=env)

returned:

type C:\Users\Stoves\Desktop\abcd.txt   | msg /time:60 /w Stoves
Message from STDIN is empty; no message sent

Can you elaborate further on what you are doing? Or what else might be going on?

Secondary edit: added hello world to the abcd.txt file and running this a second time produced a popup with that message inside of it.


In light of os.system("C:\\Windows\\System32\\msg.exe * test") working change:

type C:\abcd.txt | msg /time:60 /w %username%

to

type C:/abcd.txt | "C:/Windows/System32/msg.exe" /time:60 /w %username%

Use \\ instead of / if you want

stoves
  • 778
  • 1
  • 11
  • 25
  • Which version of python and windows are you using? – vjayky Nov 10 '14 at 22:51
  • Win 8.1 Pro, Python 2.7.7 – stoves Nov 10 '14 at 22:56
  • I just tried **os.system("C:\\Windows\\System32\\msg.exe * test")** on 64 bit version of python 2.6 and it works. – vjayky Nov 10 '14 at 23:09
  • since that works you should be able to modify the batch file to run msg directly. answer updated, give that a shot. – stoves Nov 10 '14 at 23:22
  • Sorry for the confusion (with too many comments). The problem is not with full path not being mentioned. 64-bit version of python works even without the path. i.e., **os.system("msg * test")**. I use 32-bit version of python for other reasons and want to get this working on 32-bit if that is possible. Thanks for your help! – vjayky Nov 10 '14 at 23:27
  • if you have 32 bit version of python, can you check if **os.system("msg * test")** works? You may be able to replicate the problem. – vjayky Nov 10 '14 at 23:28