New submission from Guy Mott: 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)] ---------- components: Extension Modules, Windows files: buggy.py messages: 57952 nosy: Quigon severity: major status: open title: os.system() fails for commands with multiple quoted file names type: crash versions: Python 2.5 Added file: http://bugs.python.org/file8828/buggy.py __________________________________ Tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue1524> __________________________________
#=============================================================================== # This is example logic for Python code that depends upon an external executable # file. If the executable is found on the system path then no problem. # Otherwise, this script attempts to execute the executable in the same # directory where the script is located. # Unfortunately, this logic fails because os.system() fails when both the # executable and the output files are both quoted. Note that the command lines # that this code constructs work just fine at the command prompt. They fail in # os.system(). #=============================================================================== import os.path import sys strCmdName = "ListMetadata" # name of our executable file strOutName = "20071129Metadata.txt" # name of output file # Command line is constructed with both file names quoted for maximum generality. strCmdLine = '"%s" > "%s"' % (strCmdName, strOutName) print "strCmdLine=[%s]" % strCmdLine nRtn = os.system(strCmdLine) # if os.system() fails (which it does) ... if (nRtn != 0): print 'Could not find "%s" on path, looking in script directory' % strCmdName # Create a new command line with a full executable file specification in our script directory. strCmdLine = '"%s" > "%s"' % (os.path.join(os.path.dirname(sys.argv[0]), strCmdName), strOutName) print "strCmdLine=[%s]" % strCmdLine nRtn = os.system(strCmdLine) if (nRtn != 0): raise Exception("Could not locate command")
_______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com