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

Peter Otten __peter__ at web.de
Wed Apr 29 16:11:38 CEST 2015


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.
"""

> 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
> 
> 
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> All right, but apart from the sanitation, the medicine, education, wine,
> public order, irrigation, roads, a
> 
> fresh water system, and public health, what have the Romans ever done for
> us?
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list