简体   繁体   English

如何使用基于OS版本的python遍历目录?

[英]How do I walk a directory using python based on OS version?

I am trying to compare a file stored in a list to the same file located on a server that may be W2K3 or W2K8. 我正在尝试将列表中存储的文件与服务器上可能位于W2K3或W2K8的同一文件进行比较。 I am trying to use a single function to this but am stuck at this if statement. 我试图对此使用单个函数,但被卡在此if语句中。 I am trying to figure out how to get the proper directory path into os.walk(): 我试图弄清楚如何将正确的目录路径放入os.walk():

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

I did have this working before but had repeating lines so I wanted to simplify the code. 我以前确实有过这项工作,但是重复了几行,所以我想简化代码。 Here is the full class: 这是完整的课程:

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)

Your question is a bit unclear, and your sample code obviously isn't your real code (you can't call os.walk with no params), but I think I can guess what you're after. 您的问题还不清楚,您的示例代码显然不是您的真实代码(您不能在没有参数的情况下调用os.walk),但是我想我可以猜到您想要的是什么。

First, change fileCheck like this: 首先,像这样更改fileCheck

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

Now, in __init__ , do the last part like this: 现在,在__init__ ,像这样做最后一部分:

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

Here is the snipit of my updated, functional code. 这是我更新的功能代码的片段。

        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):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM