[python-win32] Trying to run an external program
John Machin
sjmachin at lexicon.net
Thu Sep 21 01:21:48 CEST 2006
On 21/09/2006 8:03 AM, Brant Sears wrote:
> Hi. I'm new to Python and I am trying to use it on Windows XP. I am trying to use it to replicate the same thing that I do on Linux and Mac via shell scripting (Batch files aren't powerful enough, so I've decided to try to use Python.)
>
> What I want to do is execute a program and have the results of the execution assigned to a variable. According to the documentation the way to do this is as follows:
>
> import commands
> x = commands.getstatusoutput('dir')
>
> This should assign "x" to be the output of the command "dir". However, when I run this (or any other command), x ends up being:
>
> (1, "'{' is not recognized as an internal or external command,\noperable program or batch file.")
Actually, the output is defined to be a tuple (exitstatus,
command_result). The exitstatus is 1, and the "is not recognised" thing
is the result of the command, which was evidently "{" (!!)
>
>>From looking through the documentation, I'm believing that the implementation of commands.getstatusoutput is actually some multi-step thing that starts with issuing the bracket character that is being choked on. This leads me to believe that Python or perhaps just the commands module is not setup correctly on my computer.
>
> I installed Python using the Python2-5.msi link that I found at:
> http://www.python.org/download/releases/2.5/
>
> I left everything the default during installation, so Python was installed to C:\Python25. The only other thing I did was add this PATH variable on my computer.
>
> Any ideas on what I might do to troubleshoot this?
>
(1) Read the manual: "Availability: Unix"
(2) Read line 25 of the source code (c:\python25\lib\commands.py)
Others may know of canned Python solutions for Windows; all I can
suggest is to write something like the (very short) commands module,
using the functions in the subprocess module.
HTH,
John
More information about the Python-win32
mailing list