How to get a network resource on a windows server

Tim Golden tim.golden at viacom-outdoor.co.uk
Thu Dec 1 03:49:42 EST 2005


[Frank.Lin]

> I am working in windows environment and I 
> want copy some files from a remote share folder.

> Now I want to let my script establish a 
> connection between NULL device name and a 
> shared resource then return a list of files 
> matching the given pattern and copy them
> to local host

It's not entirely clear exactly what you're trying
to do. I'll present some (hopefully) useful tips
based on a bit of guesswork. I'm assuming you're
familiar with the various ways of copying things
around (copy, xcopy, robocopy, Python's shutil
module) but feel free to come back and ask if
that's where the stumbling block is.

WARNING: Code snippets are untested.

--------------------------------------------
Guess 1) 
You're trying to copy, eg, \\server\share\*.txt 
to somewhere local, eg c:\temp.

You could simply let the OS do the work for you:

<code>
import os

os.system (r"copy \\server\share\*.txt c:\temp")
</code>


--------------------------------------------
Guess 2)
You're trying to do something a bit more fancy
which requires you to make a local connection
to the remote share. NB You *don't* need to do
this just to copy files, but let's imagine you
have some other purpose in mind.

You can use the win32wnet module from the pywin32
extensions to map the drive, and then use the
os or any other technique to copy the files.

<code>
import win32wnet
import win32netcon

win32wnet.WNetAddConnection2 (
  win32netcon.RESOURCETYPE_DISK,
  "Z:",
  r"\\server\share",
  None,
  None,
  None,
  0
)

# then os.system (r"copy z:\*.txt c:\temp") or whatever
</code>

--------------------------------------------
Guess 3)
You need to create an authenticated connection
using the NULL device. (Re-reading, this is
the most likely interpretation of your question).

Use the win32wnet module again, this time passing
in no device, but adding a username/password

<code>
import win32wnet
import win32netcon

win32wnet.WNetAddConnection2 (
  win32netcon.RESOURCETYPE_DISK,
  None,
  r"\\server\share",
  None,
  "username",
  "password",
  0
)

# then os.system (r"copy \\server\share\*.txt c:\temp") or whatever
</code>


________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list