Why character '^' is ignored byt Python Popen - how to escape '^' character in Popen Windows?

后端 未结 1 1430
你的背包
你的背包 2020-11-30 13:38

I prepared some code to execute such command line:

c:\\cygwin\\bin\\convert \"c:\\root\\dropbox\\www\\tiff\\photos\\architecture\\calendar-bwl-projekt\\bwl01         


        
相关标签:
1条回答
  • 2020-11-30 14:13

    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.

    0 讨论(0)
提交回复
热议问题