‘^’ is ignored by Python – how to escape ‘^’ character in Popen Windows?

Why Python cuts ‘^’ character and how to avoid it?

Python does not cut ^ character. Popen() passes the string (resize_command) to CreateProcess() Windows API call as is.

It is easy to test:

#!/usr/bin/env python
import sys
import subprocess

subprocess.check_call([sys.executable, '-c', 'import sys; print(sys.argv)'] +
                      ['^', '<-- see, it is still here'])

The latter command uses subprocess.list2cmdline() that follows Parsing C Command-Line Arguments rules to convert the list into the command string — it has not effect on ^.

^ is not special for CreateProcess(). ^ is special if you use shell=True (when cmd.exe is run).

if and only if the command line produced will be interpreted by cmd, prefix each shell metacharacter (or each character) with a ^ character. It includes ^ itself.

Leave a Comment