I am looking for some help on logic, the code is not very Pythonic. I am still participating. We constantly display drive Z: in different locations. Here is what I'm trying to accomplish
1: check the old card on Z: say \ 192.168.1.100 \ old
2: Map the new location to Z: say \ 192.168.1.200 \ new
3: Verify that a new Z: mapping exists and is still connected. 4: If it is disconnected or not connected, reconnect it and write it down
90% of the code works, if I run it as it is, it cancels the old disk and maps the new disk, but the name of the old disk remains unchanged, even if it is mapped to a new location, and I can view it. Another problem is only that I want to run checkOldDrive once and just run checkDrive . Any advice is appreciated.
#!/usr/bin/python import pywintypes import win32com.client import os.path import sys import string import fileinput import time import win32net ################################################################## # Check for old Z: map and remove it # Map the new instance of Z: # Check if the Z: drive exists # if the drive exists report to status.log we are working # if the drive DOES NOT exist map it and report errors to the log ################################################################### def checkDrive(): if os.path.exists('z:'): saveout = sys.stdout fsock = open('status.log', 'a') sys.stdout = fsock print os.getenv("COMPUTERNAME"), " - ", time.ctime(), " - Connected" sys.stdout = saveout fsock.close() else: ivvinetwork = win32com.client.Dispatch('Wscript.Network') network_drives = ivvinetwork.EnumNetworkDrives() for mapped_drive in [network_drives.Item(i) for i in range(0, network_drives.Count() -1 , 2) if network_drives.Item(i)]: ivvinetwork.RemoveNetworkDrive(mapped_drive, True, True) drive_mapping = [ ('z:', '\\\\192.168.1.100\\newmap', 'someuser', 'somepass')] for drive_letter, network_path, user_name, user_pass in drive_mapping: try: ivvinetwork.MapNetworkDrive(drive_letter, network_path, True, user_name, user_pass) saveout = sys.stdout fsock = open('status.log', 'a') sys.stdout = fsock print os.getenv("COMPUTERNAME"), " - ", time.ctime(), " - ", drive_mapping, "Drive Has Been Mapped" sys.stdout = saveout fsock.close() except Exception, err: saveout = sys.stdout fsock = open('status.log', 'a') sys.stdout = fsock print os.getenv("COMPUTERNAME"), " - ", time.ctime(), " - ", err sys.stdout = saveout fsock.close() def checkOldDrive(): if os.path.exists('z:'): ivvinetwork = win32com.client.Dispatch('Wscript.Network') network_drives = ivvinetwork.EnumNetworkDrives() for mapped_drive in [network_drives.Item(i) for i in range(0, network_drives.Count() -1 , 2) if network_drives.Item(i)]: ivvinetwork.RemoveNetworkDrive(mapped_drive, True, True) checkOldDrive() checkDrive()