Eryk Sun <eryk...@gmail.com> added the comment:
A literal backlash has to be escaped by doubling it if it precedes a double quote, else it escapes the double quote character. This is how typical command-line argument parsing handles backslash in Windows [1]: * 2n backslashes followed by a quotation mark produce n backslashes followed by begin/end quote. This does not become part of the parsed argument, but toggles the "in quotes" mode. * (2n) + 1 backslashes followed by a quotation mark again produce n backslashes followed by a quotation mark literal ("). This does not toggle the "in quotes" mode. * n backslashes not followed by a quotation mark simply produce n backslashes. For example: import ctypes shell32 = ctypes.WinDLL('shell32', use_last_error=True) shell32.CommandLineToArgvW.restype = ctypes.POINTER(ctypes.c_wchar_p) n = ctypes.c_int() Escape the trailing backslash as a literal backslash: >>> cmd = r'/quiet TargetDir="D:\pyt hon\\" AssociateFiles=0' >>> args = shell32.CommandLineToArgvW(cmd, ctypes.byref(n)) >>> args[:n.value] ['/quiet', 'TargetDir=D:\\pyt hon\\', 'AssociateFiles=0'] Escape the double quote as a literal double quote: >>> cmd = r'/quiet TargetDir="D:\pyt hon\" AssociateFiles=0' >>> args = shell32.CommandLineToArgvW(cmd, ctypes.byref(n)) >>> args[:n.value] ['/quiet', 'TargetDir=D:\\pyt hon" AssociateFiles=0'] --- [1] https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw ---------- components: +Windows nosy: +eryksun, paul.moore, steve.dower, tim.golden, zach.ware _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue45073> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com