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

Dave Angel davea at davea.name
Wed Apr 29 16:02:12 CEST 2015


On 04/29/2015 08:47 AM, 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?
>
> 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
>

Just a comment about Windows.  There is a current directory for each 
lettered drive partition.  But a unc name is not necessarily on any 
known drive.

And if executable, infile and outfile might have spaces within them, you 
need to quote them as well.




-- 
DaveA


More information about the Tutor mailing list