I am seeing an annoying trailing double quote when passing in a quoted directory in Windows to Python.
This is my Python test program, print_args.py
:
import sys
print(sys.argv)
This is what I get when I run it from the command line. Note that the quoted directory is the standard format generated by tab completion in the Windows shell. The double quotes are needed because of the spaces in the path.
>py print_args.py -test "C:\Documents and Settings\"
['print_args.py', '-test', 'C:\\Documents and Settings"']
The trailing backslash has been replaced with a double quote, presumably because Python is reading it as a quoted double quote, rather than matching it to the leading quote.
If instead of passing the parameter to Python, I pass it to a batch script which just echoes it, then I get the trailing backslash as expected.
So somewhere between the CMD shell and Python seeing sys.argv there has been some parsing which has affected backslashes and double quotes.
Can anyone illuminate?
Edited to add:
Further reading suggests to me that Python is doing some parsing of the windows command line arguments to construct sys.argv. I think Windows passes the entire command line string, in this case mostly unchanged, to Python and Python uses its own internal logic to break it into the strings in the sys.argv list. This processing must allow escaped double quotes as a special case. I would be pleased to see some documentation or the code...