我在找一些逻辑方面的帮助,代码不是很像Python,我还在学习。我们把Z:开车一直映射到不同的地点。这就是我要做的
1:检查Z轴上的旧地图:例如\192.168.1.100\old
2: 将新位置映射到Z:例如\192.168.1.200\new
3: 确保新的Z:映射存在且仍处于连接状态
4: 如果它断开连接或未映射,请重新连接并记录
90%的代码有效,如果我按原样运行,它会取消映射旧驱动器并映射新驱动器,但旧驱动器的名称保持不变,即使它映射到新位置,我可以浏览它。另一个问题是我只想运行一次checkoldrive然后让checkDrive运行。如有任何建议,我们将不胜感激。在
#!/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()
我根据你安排的剧本编了一个剧本,我相信它完成了你所描述的。在
我试图用一种既有python风格又遵循良好编程原则的方式来实现它。在
特别是,我做了以下工作:
当然,还有一些改进的空间,但我已经测试了这个脚本,它是功能性的。它应该提供一些好的教训,同时也帮助你完成你的任务。享受吧。在
相关问题 更多 >
编程相关推荐