Given a call to os.system() with a command string like this:
os.system('"TheCommand" > "MyOutput"') # fails
then the call fails with the following error message on the console:
'TheCommand" > "MyOutput' is not recognized as an internal or
external command, operable program or batch file.
Note that both the executable file name and the redirected output file
name are quoted.
Apparently the command string is being parsed and the first quote is
incorrectly being matched with the last quote. A more general statement
of this bug might say that multiple quoted fields on a command line
cause os.system() to fail. I have not done enough research to better
characterize the problem.
By contrast, if only one of the file names is quoted then the call to
os.system() succeeds. E.g., these calls succeed:
os.system('TheCommand > "MyOutput"') # succeeds
os.system('"TheCommand" > MyOutput') # succeeds
Of course this is a simplified example where it is not necessary to
quote either file name. Real world examples include 2 file names with
imbedded spaces. E.g.:
os.system('"The Command" > "My Output"') # fails
'The' is not recognized as an internal or external command, operable
program or batch file.
A further real-world example is a command line with full path
specifications for both the executable file and the output file. Such
path specifications may include imbedded spaces so both need to be
quoted. However, quoting both causes os.system() to fail. E.g.:
os.system(r'"C:\New Folder\TheCommand" > "C:\New Folder\MyOutput"')
# fails
'C:\New' is not recognized as an internal or external command,
operable program or batch file.
The above described scenario is the situation in the attached script
that includes logic for finding an executable file that may not be
found on the system path but is co-located with the Python script file.
Thus the script and its companion file(s) may be moved from machine to
machine and will work correctly even if not in a directory that is
included on the system path. The script fails because the command line
that it constructs, with executable and output file specifications
quoted, fails in os.system().
Here is output from running the attached script:
-----------------------------------------------
C:\New Folder>buggy.py
strCmdLine=["ListMetadata" > "20071129Metadata.txt"]
'ListMetadata" > "20071129Metadata.txt' is not recognized as an
internal or external command,
operable program or batch file.
Could not find "ListMetadata" on path, looking in script directory
strCmdLine=["C:\New Folder\ListMetadata" > "20071129Metadata.txt"]
'C:\New' is not recognized as an internal or external command,
operable program or batch file.
Traceback (most recent call last):
File "C:\New Folder\buggy.py", line 16, in <module>
raise Exception("Could not locate command")
Exception: Could not locate command
-----------------------------------------------
Note that the command line that is constructed by the attached script
runs just fine and produces the desired result if it is executed
directly at a command line prompt. It is when executed via os.system()
that the command line fails.
Testing environment:
OS = Windows XP Professional
Python = 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)]
|