September 27th, 2004, 11:08 AM
Newbie help with mapping drives and stuff
I am just starting to learn python and thought I would start with a little job I need doing, I need to grab a file from another machine and copy it to my own. (both Windows machines). I would though need to authenticate on the other box though. Would it be best to map a drive from Python, or is there better ways??
Has anybody got any ideas where I start with this, I would just like a little start so I can then start building onto the program. (and hopfully learn more (I have the "Learning Python" book, so I have some hope)).
Thanks for looking and any ideas would be fantastic.
September 30th, 2004, 04:25 PM
People often seem to ask for Windows-specific tasks which can be done in Python, but not particularly elegantly.
There is a brilliant module for running commands on Linux called "commands", but it doesn't work on Windows.
The full command you want is something like:
And the module you need is os, with the spawnv function. Which is ugly and *extremely* fussy.
c:\windows\system32\net.exe use G: \\computer\share /USER:computername\username password
Try something like:
result = os.spawnv(os.P_WAIT, "c:/windows/system32/net.exe", ("net.exe", "use", "G:",
"\\\\computer\\share", "/USER:computer\\username", "password"))
if result == 0:
print "Share mapped correctly"
print "Share mapping failed"
This will wait until the command returns, so you wont be able to run it, try to copy a file, and then find it hasn't finished mapping the drive yet.
Regarding a better way: I would probably try setting up a user account on the remote computer with the same username and password that you use on the local computer, and copying the file straight with a "\\computer\share\file.name" path. (remember, you need to double the backslashes in Python, or use \\\\computer/share/file.name).