[python-win32] Mapping Drives in Python

Vernon Cole vernondcole at gmail.com
Fri Mar 4 10:17:53 CET 2011


I've never used robocopy -- never heard about it until your post --
but it sounded funny to me that it should need to use drive letter
mappings. Microsoft has been moving away from them since MS DOS days.
Sure enough, this shiny new command line utility can be used with
server share names as well as drive letters.

I tried typing "robocopy /?" on my Vista machine, and I get this:
v v v v v
              Usage :: ROBOCOPY source destination [file [file]...] [options]

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               file :: File(s) to copy  (names/wildcards: default is "*.*").
^ ^ ^ ^ ^

Note the "or \\server\share\path" in that help text.
That would be the way to go. Use server names, not drive letters.

Remember that Python uses the backslash as an escape character in
string literals, so it takes some work to make a Windows server name.
A double backslash becomes one backslash, so to get two you need to
type four. "\\\\someserver\\someshare\\somepath".
It's easier to use the "raw string" feature by putting an "r" before
the opening quote.
r"\\someserver\someshare\somepath"
in Python.
--
Vernon Cole

On Fri, Mar 4, 2011 at 12:46 AM, Becky Mcquilling
<ladymcse2000 at gmail.com> wrote:
> Hi:
> Hoping you guys can help out a python noob here.
> I have a list of machines that I am backing up flat files from to one
> central machine.  I need to:
> a)  Map Drives and b) run robocopy
> I am iterating through a text file that has the share names and the and
> directory that I want to be created:
>
> count = 0
> alphabet = 'klmnopqrstuv'
> today = datetime.date.today()
> backup_servers = {}
> f = open('c:/test/backup_shares.txt')
> for line in f:
>     backup_server = line.split(',')
>     backup_servers[ backup_server[0]]=backup_server[1]
> for i, v in backup_servers.items():
>     backup_shares = i
>     archive_dir = v.strip()
>     archive_dir += str(today)
>     drive_letter = alphabet[count]
>     count += 1
>
> The above creates a dictionary and I iterate through that.
> backup_shares = '\\server_name\c$' for example and archive_dir is the
> directory on the local machine that I will create with a timestamp to backup
> the files to on the local machine example "d:\backup_dir\03-03-2011\files
> What I want to do is map a drive, which is why the alphabet string.  I
> iterate through that and then get k first backup dir, etc.
> I'm not sure what method to call to map a drive or how the best way to call
> robocopy is.  I don't have the choice of using IronPython.  I have the win32
> modules.
> And this works:
>  win32net.NetUseAdd(None,1,{'remote':r'\\server_name\c$','local':'K:'})
> However, I want to use the variable backup_server and insert the
> drive_letter variable in the script snippet, but I haven't been able to make
> that work.  Any suggestions?
> Also, suggestions and examples on calling robocopy?
> Thanks so much!
> Becky
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>
>


More information about the python-win32 mailing list