Access Network Drive's With Password In Python

Access Network Drive's With Password In Python

In this tutorial, I will explain you all the Guys about how you can access in drive’s by python programming language.


Sometime’s happened,when your system memory is full or you want  to use some accessing type operation’s  int two system’s.Here we learn how we create a program in python programming language, that use to access in other drive’s with a password for safety.

So let’s start our tutorial and I will try to make program it easy and understandable to you.


import os
import os.path
import shutil
import sys
import win32wnet

First of all, we have to Import some libraries and modules like os, shutil, sys and win32syswnet.These libraries are important because we are using these libraries later in the program.

def access_drive(host, source, destination, username=None, password=None, move=False):

                        wnet_connect(host, username, password)

                       destination = covert_unc(host, destination)

                      if not dest_dir[len(destination) - 1] == '\\':
                                  destination = ''.join([destination, '\\'])

                     if not os.path.exists(destination):


                         if not os.path.isdir(destination):

                        if move:
                              shutil.move(source, destination)


                             shutil.copy(source, destination)

Now create a function called access_drive,that have some attributes host, source, destination,        username, password and move = false.After that we use wnet_connect to connect host with username and password and create a destination value.

def delete(host, path, username=None, password=None):

                                 wnet_connect(host, username, password)

                                 path = covert_unc(host, path)

                                if os.path.exists(path):

                                        if os.path.isfile(path):


Our next step is create a delete function which have some attributes.we again do the same things that we do on above function just one difference we have called os.remove().This thing will remove path.

def move(host, source, destination, username=None, password=None):
              return copy(host, source, dest_dir, username, password, True)

Move to next step, here we create a move function,where we move the copy the details and move the connection.

def replace(host, path):

              return ''.join(['\\\\', host, '\\', path.replace(':', '$')])

Here we replacing our details in replace function.

def connect(host, username, password):
                         data = ''.join(['\\\\', host])
                               win32wnet.WNetAddConnection2(0, None, data, None, username, password)
                       except Exception, err:
                                if isinstance(err, win32wnet.error):

                                               if err[0] == 1219:
                                                win32wnet.WNetCancelConnection2(data, 0, 0)
                      return wnet_connect(host, username, password)
                      raise err

Now we create a connect function, where we which will create a connection here and all the details is shown on above function.

if __name__ == '__main__':

                    copy('w0001', 'c:\\documents', 'c:\\transferred')

                   move('w0001', 'c:\\documents', 'c:\\transferred', 'admin', 'adminpass')

                  delete('w0001', 'c:\\transferred', 'testdom\\user1', 'user1pass')

                 accounts = [
                                      ('administrator', 'root'),
                                     ('desktopeng', '******'),
                                     ('user\\administrator', '****')]

                computers = ['c01', 'c02', 'c03', 'c04', 'c05', 'c006']

               auth_failed = []
                transfer_failed = []

             for computer in computers:

                          for account in accounts:
                                   username, password = account
                              wnet_connect(computer, username, password)
                          except Exception, err:

                             if isinstance(err, win32wnet.error) and err[0] == 1326:
                          if account == accounts[len(accounts) -1]:
                            copy(computer, 'c:\\documents', 'c:\\transferred')
                        except Exception, err:
                                print (err)

print ('Authentication failure: %s' % (str(auth_failed).strip("[]'")))
print ('Unknown failure: %s' % (str(transfer_failed).strip("[]'")))

At the last step, we execute our python program.we call all the functions that we defined above.__main__ is a type of main class same as Java main class.We have to just execute our python program in the system.In the account list we have to fill our administration details like our Os name password to connect with other device, It's very mandatory for connection purpose.

After that we can easily access to any drive of the system with a password and all this work is done in python programming language.


Now we have Created our program in python and Iam really enjoy to create and explain it to you all Guys.I hope you also enjoys to create program.I know we have to install some extra libraries like  win32syswnet, but we have to install it If we want to make this program.

So Today Tutorial on Access Network Drive with Password in Python is over……..Shortly, I will be available with a new intresting topic till then bye bye➤➥➞….

                            Other Links 

>>> Python Basics                                      >>> Data Structure in Python

>>> CRUD/CURD in Django                   >>> Job Opportunities in Python

>>> Currency Detection By Image in Python     

                                                                     >>> Pandas Tutorials


Post a Comment