passing directory with spaces in it from raw_input to os.listdir(path) in Python
<p>I've looked at a few posts regarding this topic but haven't seen anything specifically fitting my usage. <a href="http://stackoverflow.com/questions/36555950/spaces-in-directory-path-python">This one looks to be close</a> to the same issue. They talk about escaping the escape characters.</p>

<p>My issue is that I want this script to run on both MAC and PC systems - and to be able to process files in a sub-folder that has spaces in the folder name.</p>

<p>Right now I use this code gleaned partially from several different SO posts:</p>

<pre><code>directory = raw_input("Enter the location of the files: ")
path = r"%s" % directory

for file in os.listdir(path):
</code></pre>

<p>I don't quite understand what the second line is doing - so perhaps that's the obvious line that needs tweaking. It works fine with regular folder names but not with spaces in the name.</p>

<p>I have tried using "\ " instead of just " ", - that didn't work - but in any case I'm looking for a code solution. I don't want the user to have to specify an escape character</p>

<p>On Windoze using sub folder name "LAS Data" in response to raw_input prompt (without quotation marks), I get an message that:</p>

<blockquote>
  <p>the system can't find the path specified "LAS Data\*.*"</p>
</blockquote>

<p>Your problem is most likely not with the code you present, but with the input you gave. The message </p>

<blockquote>
  <p>the system can't find the path specified "LAS Data\*.*"</p>
</blockquote>

<p>suggest that you (or the user) entered the directory together with a wildcard for files. A directory with the name "LAS Data\*.*" indeed does not exist (unless you did something special to your file system :-).</p>

<p>Try entering just "LAS Data" instead.</p>

<p>The raw string should not be needed either</p>

<pre><code>&gt; python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import os
&gt;&gt;&gt; directory = raw_input("Enter the location of the files: ")
Enter the location of the files: Directory with Spaces
&gt;&gt;&gt; for file in os.listdir(directory):
...     print(file)
...
hello-1.txt
hello-2.txt
&gt;&gt;&gt;
</code></pre>

python python-2.7 escaping