[Tutor] subprocess.Popen(..., cwd) and UNC paths

Albert-Jan Roskam fomcl at yahoo.com
Wed Apr 29 18:54:11 CEST 2015


-----------------------------
On Wed, Apr 29, 2015 4:11 PM CEST Peter Otten wrote:

>Albert-Jan Roskam wrote:
>
>> Hello,
>> 
>> Windows has the 'feature' that the CD command does not work with UNC
>> paths. So in the code below, I cannot use the 'cwd' parameter of
>> subprocess.Popen. Therefore I use pushd/popd to accomplish the same
>> effect. Is there a better way to do this? (other than using full path
>> names everywhere, or os.chdir). Would it be a useful improvement of Python
>> itself if cwd also works with UNC path?
>
>What is the error you get? I don't have Windows to verify, but a quick look 
>into the subprocess source suggests that it uses CreateProcess().
>Googling for that finds
>
><https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx>
>
>which states
>
>""
>lpCurrentDirectory [in, optional]
>The full path to the current directory for the process. The string can also 
>specify a UNC path.
>""

Hmmm, that sounds pretty convincing indeed (makes it even stranger that CD works the way it works). I believe it threw a WindowsError, indicating that the file(s) could not be found, because the dir was not changed. I actually first ran into this problem with this script, so with my current script I immediately refrained from using cwd: http://code.activestate.com/recipes/578883-git-pre-commit-hook-to-reject-large-files-using-py/
Git gave a fatal error in windows and the pushd/popd fixed it.

>> import sys
>> import os
>> import getpass
>> import subprocess
>> 
>> path = r'\\server\share\possibly with\space'
>> executable = 'blah.exe'
>> username = os.getenv("USERNAME")
>> password = getpass.getpass("Enter password: ")
>> infile =  sys.argv[1]
>> outfile = sys.argv[2]
>> cmds = ['pushd "%s" &&' % path, executable, username, password, infile,
>> outfile, "&& popd"]
>> 
>> result = subprocess.Popen(" ".join(cmds), shell=True)
>> error = result.stderr
>> if error:
>>     raise RuntimeError(error.read())
>> 
>> Regards,
>> 
>> Albert-Jan
>> 
>> PS: Python 2.7 on Windows 7 32



More information about the Tutor mailing list