Pregunta

First time questioning here:

I have a need to map a network drive in windows. The location is an internal sharepoint document library.

In the cmd window:

net use g: http://na.com/DMP/DMP/programming/

is successfull --> the command completed succeffuly

os.system('"net use k: http://na.com/DMP/DMP/programming/"')

is also successful.

However i would like to use subprocess.call in the event that the drive is already mapped - i would like to try another drive

call(["net", "use", ":q", '"http://na.com/DMP/DMP/programming/"'])

This fails with "System error 67 has occured. The network name cannot be found" I have tried many options for the last list item with no luck.

Any idea what I can stuff in there to have this complete successfully or a different method to map drives.

¿Fue útil?

Solución

There are at least two problems in your code:

call(["net", "use", ":q", '"http://na.com/DMP/DMP/programming/"'])

First, you've got ":q" where you meant "q:". This might cause the net command to interpret :q as your network location instead of your target drive, which could cause an error 67.

Second, you've got an extra set of quotes around the URL: '"http://na.com/DMP/DMP/programming/"' where you should be using 'http://na.com/DMP/DMP/programming/'. When subprocess builds the string to pass to CreateProcess, it already quotes each of your parameters. So, if you quote them yourself, you end up double-quoting the parameters. There are some cases where this is actually not possible in Windows, so you end up with garbage, but I don't think that's the case here. You will successfully get this quoted string to net, telling it that you want to open either a relative path starting with "http: or a URL with protocol "http, or something like that. Whatever it is, it's not a usable network location, which most likely will cause an error 67.

As Ben pointed out, your system call has a similar problem—you put an extra pair of quotes around the entire string. If you really wanted to figure it out, there probably is some reason that this worked… but I don't think you want to figure it out. Just take it as "I did the wrong thing, but I got lucky", and don't do it that way in the future.

Finally, as the documentation says:

On Windows, an args sequence is converted to a string that can be parsed

This means that, if you already have a working command line for Windows, you're better off just using it as a string, than trying to break it down into a sequence for subprocess to reassemble.

(Keep in mind that this is only true for Windows! On other platforms, instead of building a command line string to pass to a function in the CreateProcess family, subprocess builds an array of strings to pass to a function in the exec family.)

So, just do this:

call("net use g: http://na.com/DMP/DMP/programming/")
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top