Passing a valid path to Python from PHP
<p>I have a Python program that parses files, takes a path as and argument and parses all files in the given path and all sub directories - using <code>os.walk(path)</code>. I want to call this from my php Web App, so the user can specify a path, which is then passed as an argument to the parser. (Passing a path is ok because its all on an internal network).</p>

<p>I can call the parser fine and pass the arguments ok using <code>popen()</code>, but the path that the Python program receives is always invalid. I have had the php script output the command it is sending to the browser. If I copy and paste that command into a command window, the parser works fine.</p>

<p>I know the path the php script passes is invalid from the result of <code>os.path.exists(path)</code> in the Python script</p>

<p>This is the code to call the Python program:</p>

<pre><code>$path = $_REQUEST['location'];
echo "Path given is: ".$path;
$command = 'python C:\Workspaces\parsers\src\main\main.py '. intval($mode).'  "'.$path.'"';
echo "&lt;p&gt;".$command."&lt;/p&gt;";
$parser = popen($command, 'r');
if ($parser){
  echo "&lt;p&gt;Ran the program&lt;/p&gt;";
  while (!feof($parser)){
    $read = fgets($parser);
    if (!$read)
      echo "&lt;p&gt;Reached end of file&lt;/p&gt;";        
      else
        echo "&lt;p&gt;".$read."&lt;/p&gt;";                 
    }
  }
</code></pre>

<p>The command echoed in the browser is like:</p>

<p><code>python C:\Workspaces\parsers\src\main\main.py 2 "I:\Dir1\Dir2\Dir3"</code></p>

<p>Where the 2 is just another argument to the script and <code>$_REQUEST['location']</code> is defined from an input text box in a form on the calling page.</p>

<p>This is on a Windows system, so I am assuming this has something to do with the backslashes in the path.</p>

<p>Basically, I am unsure as to how all the backslashes are being handled. I would like to understand how strings containing backslashes are sent to the php page, and how they are sent again using <code>popen()</code>. I think the result being printed to the browser is not the raw command string, and I can't be sure how many backslashes are really in the command being issued by popen().</p>

<p>If anyone has any ideas I'd really appreciate it.</p>

<p>Edit:</p>

<p>So in the Python program the path is used as follows:</p>

<pre><code> nfiles=0
 print 'Parsing all files in directory tree '+path+"&lt;br /&gt;"
 start = time.time()
 if not os.path.exists(path):
   print "&lt;p&gt;Path is NOT REAL!!!&lt;/p&gt;"
 else:
   print "&lt;p&gt;Path IS real!&lt;/p&gt;"
 for root, dirs, files in os.walk(path):
   for f in files:
     file = os.path.join(root,f)
     print file
     nfiles+=1
     ...Code to run parser...
 print nfiles, "Files parsed&lt;br /&gt;"
</code></pre>

<p>This is echoed back to the browser from the $read variable.</p>

<p>Output of that is:</p>

<pre><code>Parsing all files in directory tree I:\Dir1\Dir2\Dir3
Path is NOT REAL!!!

0 Files parsed
</code></pre>

<p>This is identical to the output if the command is run from the command line (the command being copied from the browser and pasted into the cmd window). EXCEPT, when run that way the path IS real, and all the files are parsed. (and in the command window the html markup shows too)</p>

<p>The web server and parsers are hosted on my local machine.</p>
