0

我正在尝试将存储在列表中的文件与位于可能是 W2K3 或 W2K8 的服务器上的同一文件进行比较。我正在尝试对此使用单个函数,但我被困在这个 if 语句中。我试图弄清楚如何将正确的目录路径放入os.walk():

    if osVer == 'serverW2k3':
        continue
    elif osVer == 'serverW2k8':
        for folder, subfolders, files in os.walk():

我之前确实有这个工作,但有重复的行,所以我想简化代码。这是完整的课程:

class checkstatus:
    def __init__(self):
        print 'Checking Status...'
        chkbkpstats = csv.reader(file('c://temp//networkerservers.csv'))

        srvrs = []
        for row in chkbkpstats:
            srvrs.append({'Name' : row[0], 'Instance' : row[1]})

        for srvr in srvrs:
            srvrName = (srvr['Name'])
            srvrInst = (srvr['Instance'])
            w2k3Chk = r'\\%s\d$\DA$Utils\log\networker' % srvrName
            w2k8Chk = r'\\%s\c$\ProgramData\SQL\DA$Utils\log\networker' % srvrName

            try:
                c = wmi.WMI(srvr['Name'])

            except:
                print 'Error connecting to %s to check OS version' % srvrName

            else:
                osVer = c.Win32_OperatingSystem()[0].Caption
                if '2003' in osVer:
                    return 'serverW2k3'
                    #self.fileCheck(w2k3Chk, w2k8Chk, srvrInst, srvrName)

                elif '2008' in osVer:
                    return 'serverW2k8'
                    #self.fileCheck(w2k3Chk, w2k8Chk, srvrInst, srvrName)

    def fileCheck(self, w2k3Chk, w2k8Chk, srvrInst, srvrName, osVer):
        found = False
        if osVer == 'serverW2k3':
            continue
        elif osVer == 'serverW2k8':
            for folder, subfolders, files in os.walk():
                for sqlFile in files:
                    if sqlFile == srvrInst + ".log":
                        found = True
                        print 'The Backup For %s on %s Still Running' % (srvrInst, srvrName)
                    elif sqlFile == (srvrInst + ".ok"):
                        found = True
                        print 'The Backup For %s on %s Completed Successfully' % (srvrInst, srvrName)
                    elif sqlFile == (srvrInst + ".err"):
                        found = True
                        print 'The Backup For %s on %s Has Failed' % (srvrInst, srvrName)
                if not found:
                    print 'No file for %s found on %s' % (srvrInst, srvrName)
4

2 回答 2

0

你的问题有点不清楚,你的示例代码显然不是你的真实代码(你不能在没有参数的情况下调用 os.walk),但我想我可以猜到你在追求什么。

首先,fileCheck像这样改变:

def fileCheck(self, path, srvrInst, srvrName):
    found = False
    for folder, subfolders, files in os.walk(path):
        ...

现在,在 中__init__,像这样执行最后一部分:

else:
    osVer = c.Win32_OperatingSystem()[0].Caption
    if '2003' in osVer:
        self.fileCheck(w2k3Chk, srvrInst, srvrName)
    else:
        self.fileCheck(w2k8Chk, srvrInst, srvrName)
于 2012-09-18T23:09:24.257 回答
0

这是我更新的功能代码的片段。

        else:
            osVer = c.Win32_OperatingSystem()[0].Caption
            if '2003' in osVer:
                self.fileCheck(srvrInst, srvrName, osVer, w2k3Chk, w2k8Chk)

            elif '2008' in osVer:
                self.fileCheck(srvrInst, srvrName, osVer, w2k3Chk, w2k8Chk)

def fileCheck(self, srvrInst, srvrName, osVer, w2k3Chk, w2k8Chk):
    found = False
    if '2003' in osVer:
        path = w2k3Chk
    elif '2008' in osVer:
        path = w2k8Chk
    for folder, subfolders, files in os.walk(path):
于 2012-09-19T13:57:37.980 回答