Jumat, 29 November 2013

Disconnect and Map Network Drive on Windows with Python

I'm using Python version 3.3.2
  1. method using subprocess.Popen

  2. # Balon Coding, 29 November 2013
    # Map network drive using subprocess.Popen
    # Drive letter: Q
    # Shared drive path: \\192.168.51.1\balon$
    # Username: balon
    # Password: coding
    
    import subprocess 
    
    # initialize
    driveLetter = 'Q:' 
    networkPath = '\\\\192.168.51.1\balon$' 
    user = 'balon' 
    password = 'coding'
    
    print('map network drive using subprocess.Popen')
    
    # Disconnect anything on drive letter Q
    winCMD = 'NET USE ' + driveLetter + ' /delete'
    print(winCMD)
    subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True)
     
    # Connect to map network drive to letter Q
    winCMD = 'NET USE ' + driveLetter + ' ' + networkPath + ' /User:' + user + ' ' + password
    print(winCMD)
    subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True)
    

  3. method using subprocess.call
  4. # Balon Coding, 29 November 2013
    # Map network drive using subprocess.call
    # Drive letter: Q
    # Shared drive path: \\192.168.51.1\balon$
    # Username: balon
    # Password: coding
    
    import subprocess 
    
    # initialize
    driveLetter = 'Q:' 
    networkPath = '\\\\192.168.51.1\balon$' 
    user = 'balon' 
    password = 'coding'
    
    print('map network drive using subprocess.call')
    
    # Disconnect anything on drive letter Q
    winCMD = 'NET USE ' + driveLetter + ' /delete'
    print(winCMD)
    subprocess.call(winCMD, shell=True)
    
    # Connect to map network drive to letter Q
    winCMD = 'NET USE ' + driveLetter + ' ' + networkPath + ' /User:' + user + ' ' + password
    print(winCMD)
    subprocess.call(winCMD, shell=True)
    

Tidak ada komentar:

Posting Komentar