1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Rep Power

    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Rep Power
    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:

    c:\windows\system32\net.exe use G: \\computer\share /USER:computername\username password
    And the module you need is os, with the spawnv function. Which is ugly and *extremely* fussy.

    Try something like:

    import os
    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).

IMN logo majestic logo threadwatch logo seochat tools logo