Escaping with backslash or quoting in the shell?
<p>When starting a program via the command line using <code>os.system(program file_argument))</code> and where the argument is a file (which might have whitespace in it) what is the best way to send the argument to the other program?</p>

<p>I've looked at these options:</p>

<ul>
<li>Using <code>pipes.quote(file_name)</code> (or <code>shlex.quote(file_name)</code> for Python 3) which produces a string like this: <code>'/dir/file with white space'</code></li>
<li>Using <code>re.escape(file_name)</code> which produces string like this: <code>/dir/file\ with\ white\ space</code></li>
</ul>

<p>Are there differences between using these that might impact the program that is called? (Are there any other options?) Anything else i need to consider?</p>

<pre><code>&gt;&gt;&gt; import subprocess
&gt;&gt;&gt; subprocess.call(['cat', 'file with space.txt'])
</code></pre>

<p>Using <code>os.system</code>, while simple to get started, breaks down when trying to do more complicated tasks.</p>

<p>I wish I had given up on <code>os.system</code> much earlier and just got familiar with <code>subprocess</code>. Well worth the time.</p>

python shell