Call subprocess.Popen() when the working directory is on a UNC path, not a mapped drive
Question: I would like to run an executable that performs some processing on a dataset
located on a remote filer. As part of the design, I'd like the location of the
filer to be flexible and something that's passed to my python program at
runtime.

I've put together the following bit of code that illustrates my problem, but
using the `python` command, so anyone can run this:

    
    
    #!/usr/bin/env python
    import os
    import subprocess
    
    def runMySubProcess(cmdstr, iwd):
        p = subprocess.Popen(cmdstr,
            shell=True,
            cwd=iwd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        (stdout, stderr) = p.communicate()
        if stderr:
            raise IOError, stderr
        return stdout
    
    if __name__ == '__main__':
        print runMySubProcess('python -h', 'C:\\')
        print runMySubProcess('python -h', '\\\\htpc\\nas')
    

This works great as long as `iwd` is on a share that's be mapped to a drive
letter on the machine. But if `iwd` is a UNC path the `subprocess.Popen()`
call ends up with stderr output, which in turn throws the IOError exception:

    
    
    Traceback (most recent call last):
      File "test.py", line 19, in <module>
        print runMySubProcess('dir', '\\\\htpc\\nas')
      File "test.py", line 14, in runMySubProcess
        raise IOError, stderr
    IOError: '\\htpc\nas'
    CMD.EXE was started with the above path as the current directory.
    UNC paths are not supported.  Defaulting to Windows directory.
    

Is there a way to make this subprocess call work _without_ resorting to
parsing `iwd` and making a temporary drive mount on the machine that exists
while the subprocess command executes? I want to avoid having to manage the
creation and cleanup of drive mounts. And of course, I'd rather not have to
deal with (albeit unlikely) case where all drive letters are currently in use
on the machine.


Answer: The problem is not with `Popen,`, but with `cmd.exe`, which does not allow the
working directory to be a UNC path. It just does not; try it. You may have
better luck specifying `shell=False` on your `Popen()` call, assuming that
whatever executable you're running can handle a UNC path, but of course if
what you're trying to run is a command that's built in to `cmd.exe` you don't
have a choice.

