当工作目录位于UNC路径上而不是映射驱动器时调用subprocess.Popen()

我想运行一个可执行文件,对位于远程文件管理器上的数据集执行某些处理。作为设计的一部分,我希望文件管理器的位置是灵活的,并且在运行时传递给我的python程序。 我把以下一些代码放在一起来说明我的问题,但使用
python
命令,所以任何人都可以运行:
#!/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')
只要
iwd
在共享上映射到机器上的驱动器号,就可以正常工作。但是如果
iwd
是UNC路径,则
subprocess.Popen()
调用以stderr输出结束,而stderr输出又会引发IOError异常:
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: '\htpcnas'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.
有没有办法让这个子进程调用工作而不需要解析
iwd
并在子进程命令执行时在存在的机器上进行临时驱动器挂载?我想避免管理驱动器安装的创建和清理。当然,我宁愿不必处理(尽管不太可能)机器上当前正在使用所有驱动器号的情况。     
已邀请:
问题不在于
Popen,
,而在于
cmd.exe
,它不允许工作目录成为UNC路径。它只是没有;试试吧。假设您正在运行的任何可执行文件都可以处理UNC路径,您可能会更好地在
Popen()
调用上指定
shell=False
,但当然如果您尝试运行的是一个内置于
cmd.exe
的命令,则您没有一个选择。     

要回复问题请先登录注册