title: Why does os.system recognise space in a path when a filename exists but need spaces escaped to give a correct error when a file doesn't exist?
body: <p>On Windows, why does os.system recognise space in a path when a filename exists but not when a file doesn't exist?</p>
  <p>Note that I know it's recommended to use subprocess, and I am somewhat familiar with subprocess.run and subprocess.Popen but i'm asking here about os.system</p>
  <p>I understand that os.system calls something similar to CMD in that the error message is the same or largely the same.</p>
  <p>I will use wordpad.exe as an example because it's on Windows systems by default, so is easy to test.</p>
  <p>wordpad.exe does exist.  zordpad.exe doesn't exist.</p>
  <pre><code>import os
  
  os.system(R'&quot;C:\Program Files\Windows NT\Accessories\wordpad.exe&quot;')
  
  os.system(R'&quot;C:\Program Files\Windows NT\Accessories\zordpad.exe&quot;')
  </code></pre>
  <p>I'm using R there to make it a raw string so that the backslash is fine for whatever path. (though I could remove the R and put \ there).</p>
  <p>And i'm using single quotes around the double quotes, so as to make the single quotes literal, so that when cmd gets called, it will see the double quotes as special and then preserve the spaces.</p>
  <p>The first line in that python code works, it opens wordpad.exe fine.</p>
  <p>The second line in that python code, trying to open &quot;zordpad.exe&quot; (a file that doesn't exist), gives an error</p>
  <pre><code>C:\blah&gt;python issuewithos_dot_system4.py
  'C:\Program' is not recognized as an internal or external command,
  operable program or batch file.
  
  C:\blah&gt;
  </code></pre>
  <p>So it breaks on the space, but only when the file doesn't exist!!!</p>
  <p>CMD doesn't have that problem</p>
  <pre><code>C:\blah&gt;&quot;C:\Program Files\Windows NT\Accessories\zordpad.exe&quot;
  '&quot;C:\Program Files\Windows NT\Accessories\zordpad.exe&quot;' is not recognized as an internal or external command, operable program or batch file.
  
  C:\blah&gt;
  </code></pre>
  <p>I could escape the space with the cmd escape character.. The escape character by cmd is ^</p>
  <p>And so the following line works</p>
  <pre><code>os.system(R&quot;C:\Program^ Files\Windows^ NT\Accessories\zordpad.exe&quot;)
  </code></pre>
  <p>(and I rightly don't use single quotes around the double quoted string above,  I don't want to have single quotes around the double quotes there, otherwise i'd be making the double quotes literal which goes to cmd(or &quot;cmd&quot;) as special and on cmd would make the caret literal, and would be a funny file path).</p>
  <p>So back to the question..</p>
  <p>Why is it that this   <code>os.system(R'&quot;C:\Program Files\Windows NT\Accessories\wordpad.exe&quot;')</code> works</p>
  <p>(imply that that syntax ensures spaces are preserved)</p>
  <p>But this <code>os.system(R'&quot;C:\Program Files\Windows NT\Accessories\zordpad.exe&quot;')</code> (trying to call an executable that doesn't exist), gives not just an error message (an error message would be expected). But it gives a broken error message, and the broken error message it gives logically implies that spaces weren't preserved.  (so it thinks <code>C:\Program</code> is the name of the executable, perhaps being passed an argument of <code>&quot;Files\...&quot;</code>)</p>
  <p>How do you explain that behaviour with the broken error message when the file doesn't exist? It's not seeing the space as literal but OK so why did it manage to see the space as literal when the filename did exist?!</p>
  <p><strong>Added</strong></p>
  <p>I spoke to a python expert that thought maybe it used the CreateProcess WinAPI function though found that it used wsystem function <a href="https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=msvc-170" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=msvc-170</a>    <a href="https://github.com/python/cpython/blob/main/Modules/posixmodule.c#L4893" rel="nofollow noreferrer">https://github.com/python/cpython/blob/main/Modules/posixmodule.c#L4893</a>    though the mystery of the broken error message remains.</p>